0

I have bunch of parent child id in array as given bellow,

 child => parent

 Array
(
  [1] => 0   ===>parent
  [2] => 1
  [4] => 1
  [5] => 4
  [6] => 2
  [7] => 0   ===>parent
  [8] => 2
  [9] => 7
  [10] => 2
  [11] => 2
  [12] => 2
  [17] => 12
  [18] => 17
  [19] => 0    ===>parent
  [20] => 19
  [21] => 20
  [22] => 20
  [23] => 20
)

And my parsing tree is

                 0->parent
     ____________|___________________________
    |                   |                   |
    1(0's child)        7                   19->0 parent's child[1]->[0],[7]->0,[19]->0
  __|_______            |                   |
  |         |           9                   20
  2         4 (2 & 4 is 1 parent's child)   |
  |         |                            21,22,23
  |         5
  |
 6,8,10,11,12
            |
            17
            |
            18

And i want to take 0 as root element id and then check for its child and grand child, And my result array is look like,

   $result = Array
        (
        [0] => 1        -->i will use my categoryname here $myarray[1]['categoryname']
        [1] => 1->2      ---> $myarray[1]['categoryname']->$myarray[2]['categoryname']
        [2] => 1->2->6   --->and so on
        [3] => 1->2->8
        [4] => 1->2->10
        [5] => 1->2->11
        [6] => 1->2->12
        [7] => 1->2->12->17
        [8] => 1->2->12->17->18
        [9] => 1->4
        [10] => 1->4->5
        [11] => 7
        [12] => 7->9
        [13] => 19
        [14] => 19->20
        [15] => 19->20->21
        [16] => 19->20->22
        [17] => 19->20->23
       )

similar like Convert a series of parent-child relationships into a hierarchical tree? this question, my code is,

  $tree = $this->to_tree($array);
function to_tree($array)
{
$flat = array();
$tree = array();
$abc = array();
  foreach ($array as $child => $parent) 
  {
       if (!isset($flat[$child])) {
        $flat[$child] = array();
       }
       if (!empty($parent)) {
        $flat[$parent][$child] =& $flat[$child];
       } else {

        $tree[$child] =& $flat[$child];
       }
  }
       return $tree;
}

And this will print hierarchical tree please help me, Thank you.

Community
  • 1
  • 1
darshan
  • 249
  • 1
  • 3
  • 12
  • Hi, I'd like to help but cannot really understand teh second (resulting) array, or, better the real relation between the 1st and 2nd. Are yoyu sure you wrote it correctly? Or maybe cound you please further explain? – Luca Reghellin Jul 31 '13 at 06:34
  • when parent id is equal to zero then its child id is store and then check for this child has another grand child id if yes then parent_id ->child_id is store and this all value is store in another array in sequence – darshan Jul 31 '13 at 06:44
  • In my child->parent array i have 3 0 element in parent and this 0 element's child id is 1,7,19 and using this id we find child id and grand child id. – darshan Jul 31 '13 at 06:52
  • @Stratboy now i print my tree pls help me if u can... – darshan Jul 31 '13 at 07:38
  • Don't have much time anymore, sorry. But still, I think it's not too clear what's your target. Would you like to have the 3rd array? And how should be builded the children? What do you mean by [15] => 19->20->21? That's a representation, not the real array shape. What's the final shape you are looking for? – Luca Reghellin Jul 31 '13 at 09:01
  • ya 3rd array and [15] => $myarray[19]['categoryname']->$myarray[20]['categoryname']->$myarray[21]['categoryname'] this is my category and this type i will display...in my view form – darshan Jul 31 '13 at 09:34

0 Answers0