This is something I have avoided for the longest time......and now I need a bit of help ???

What I am trying to do is simply build a recursive array based on parent->child, the recursion comes when their is the possibility to add any number of sub-children to sub-children so on and so forth.

Array need to run

array(4) {
  [12] => array(3) {
    ["title"] => string(7) "My Blog"
    ["parentId"] => string(1) "0"
    ["url"] => string(7) "my-blog"
  }
  [13] => array(3) {
    ["title"] => string(9) "Job Stuff"
    ["parentId"] => string(2) "12"
    ["url"] => string(9) "job-stuff"
  }
  [14] => array(3) {
    ["title"] => string(14) "More Job Stuff"
    ["parentId"] => string(2) "13"
    ["url"] => string(0) ""
  }
  [15] => array(3) {
    ["title"] => string(13) "Another Child"
    ["parentId"] => string(2) "12"
    ["url"] => string(0) ""
  }
}

to become!

array(4) {
  [12] => array(3) {
    ["title"] => string(7) "My Blog"
    ["parentId"] => string(1) "0"
    ["url"] => string(7) "my-blog"
    ["children"] => array(int) {
       [13] => array(3) {
          ["title"] => string(9) "Job Stuff"
          ["parentId"] => string(2) "12"
          ["url"] => string(9) "job-stuff"
          ["children"] => array(int) {
                [14] => array(3) {
                   ["title"] => string(14) "More Job Stuff"
                   ["parentId"] => string(2) "13"
                   ["url"] => string(0) ""
               }
          }
        [15] => array(3) {
            ["title"] => string(13) "Another Child"
            ["parentId"] => string(2) "12"
            ["url"] => string(0) ""
          }
       }
    }
}

I have taken a stab at this for about half a day and come fairly close but.....it just results in failure.

Any help would be apprected thanks!

hi,

i spend half a day to solve this.... so pls thanx me:)

<?php

$data = array(
          10=>array(
                'title' => 'AA',
                'parentId' => '0',
                'url' => 'aa'
              ),
          11=>array(
                'title' => 'BB',
                'parentId' => '0',
                'url' => 'bb'
              ),
          12=>array(
                'title' => 'AA-CC',
                'parentId' => '10',
                'url' => 'aa-cc'
              ),
          13=>array(
                'title' => 'AA-DD',
                'parentId' => '10',
                'url' => 'aa-dd'
              ),
          14=>array(
                'title' => 'AA-CC-EE',
                'parentId' => '12',
                'url' => 'aa-cc-ee'
              ),
          15=>array(
                'title' => 'AA-CC-EE-FF',
                'parentId' => '14',
                'url' => 'aa-cc-ee-ff'
              )
        );
        
        
$newData = arrayFormation($data);
echo "<pre>";
print_r($newData);
function arrayFormation($data, $parent = 0, $path = '') {
  static $i = 0;
  $i++;
  static $newData = array();
  if(!count($newData)) $newData = $data;
  static $parentArr = array();
  static $childData = array();
  foreach($data as $key => $value) {
    if($parent == $value['parentId']) {
      if($parent == 0)
        $childData[$key] = $value;
      else {
          $childData = appendChildData($childData, $key, $value);
      }
      arrayFormation($data, $key);
   }  
 }
 return $childData;
}

function appendChildData(&$childData, $key, $value) {
  static $newChilData = array();  
  foreach($childData as $childKey => $childVal) {
     if($childKey == $value['parentId']) {      
      $childData[$childKey]['children'][$key] = $value;
      return $childData;
    }
    else {    
      if(@array_key_exists('children', $childVal)) {      
        $childData[$childKey] += $childVal; 
        appendChildData($childData[$childKey]['children'], $key, $value);
      }
    }
  }
  return $childData;
}
 
?>
commented: Good job! +10

thnx for the solution :)

Here is something a bit easier and less complex :) and put into a helper class

class Helper_Index_Build_Parent_Array
{
    
    /*
     * Rearranges a array and prepares it for buildParentArray
     */ 
    
    public function prepareArrayBuild(array $array)
    {   
        foreach ($array as $k => $v)
        {
            $buildArray[$v['parentId']][$k] = $v;
        }
        
        return $buildArray;
    }
    
    public function buildParentArray(array $array, $id = 0)
    {
        $parentArray = array();
        
        foreach ($array[$id] as $k =>$v)
        {
            if (isset($array[$k])) $v['children'] = $this->buildParentArray($array, $k);
            
            $parentArray[$k] = $v;
        }
        
        return $parentArray;
    }
}

Also :)

here is the equivalent to parse that array and display them in recursive descending order.

class Helper_Admin_Blog_Array_Display
{
    
    /*
     * Builds a sorted list for parent child relations for the admin blog
     *
     * @param  array  $array
     * @return string
     */
    
    public function buildDisplay(array $array, $isChild = false)
    {
        $return = null;
            
        foreach ($array as $k => $v)
        {
            if ($isChild)
            {
                $return .= '<div class="w90"> &nbsp; |- '.$v['title'];
            }
            else
            {
                $return .= '<div class="w90">'.$v['title'];
            }
            if (is_array($array[$k]['children']))
            {
                $return .= $this->buildDisplay($array[$k]['children'], true);
            }
                
            $return .= '</div>';
        }


        
        return $return;
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.