Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having this array:
PHP
array (size=4)
  0 => 
    array (size=3)
      'name' => string '1' (length=1)
      'parent' => string '1' (length=1)
      'child' => string '1' (length=1)
  1 => 
    array (size=3)
      'name' => string '22' (length=2)
      'parent' => string '1' (length=1)
      'child' => string '22' (length=2)
  2 => 
    array (size=3)
      'name' => string '23' (length=2)
      'parent' => string '1' (length=1)
      'child' => string '23' (length=2)
  3 => 
    array (size=3)
      'name' => string '30' (length=2)
      'parent' => string '23' (length=2)
      'child' => string '30' (length=2)

I want to generate a multi level array in such a way that:
array 0 will be my root.
every other array that has their parent value equals to name value of array 0 will form a tree under the array.
e.g
array 1 and array 2 has parent=1 which is equal to name of array 0, which means they will form a direct node to array 0
but array 3 will form a node to arrar 2 because it has its parent value to equal array 2 name value.
And like that.
Please help me. I have tried several recursive functions but all effort prove abortive.
Posted
Comments
Matt T Heffron 17-Jul-15 17:41pm    
For starters, your data has a problem...
The parent of array 0 is not "some kind of null (maybe==-1)", or itself, but it is the same as its child.
This could cause some significant problems with a general purpose solution.
child == self to indicate a leaf node is also a bad design.

1 solution

Yep. Thanks to the house.
I got it solved by following this blog http://leon.vankammen.eu/tech/storing-retrieving-hierarchical-trees-between-php-and-mysql.html[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900