N
N
Noob--Saibot2017-03-17 17:21:10
PHP
Noob--Saibot, 2017-03-17 17:21:10

How to save a tree from mysql to an array in php?

The database has a table of categories with parents and children. How to display it in php as an array?
4a5767b0abad44c1b951969bf694835c.png

Answer the question

In order to leave comments, you need to log in

4 answer(s)
B
bears, 2017-03-17
@bears

For example like this sandbox.onlinephpfunctions.com/code/a3ee3d0e462cda...

M
Mikhail Vasilyev, 2017-03-17
@mickvav

If there is not a lot of data, you can do this:
1. Unload into a regular array of hashes with a regular select.
2. Set up a recursive function that receives the id of the tree node, and returns a hash whose keys are the id of the elements of the original table containing this node in the parent field, and the values ​​are links to the result of applying the same function to the key.

A
Andrew, 2017-03-23
@iCoderXXI

For the first pass, I would calculate all the relationships for each element, the entire nesting depth.
By the second pass, I generated the tree itself, decomposing the elements into it.

K
krypt3r, 2018-01-09
@psngut

<?php
$xml = simplexml_load_file('file.xml');
foreach ($xml->WMExchnagerQuerys->query as $query) {
    echo (string) $query['id'], "\t", (string) $query['amountin'], "\t", (string) $query['amountout'], "\n";
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question