2016-12-08 11 views
0

私は構造parent => childのようなツリーを持つ多次元配列を持っています。私は最後の配列ノードに子を追加したいが、私はどのように把握することはできません。下に私の配列とコードがありますが、私はちょうど詰まっています。多次元配列に子を追加する

Array 
    (
     [id] => 154 
     [text] => root 
     [parent_id] => 
     [children] => Array 
      (
       [155] => Array 
        (
         [id] => 155 
         [text] => DE 
         [parent_id] => 154 
         [children] => Array 
          (
           [157] => Array 
            (
             [id] => 157 
             [text] => Mülheim 
             [parent_id] => 155 
             [children] => Array 
              (
              )   
            ) 

           [158] => Array 
            (
             [id] => 158 
             [text] => Heißen 
             [parent_id] => 155 
             [children] => Array 
              (
              )   
            ) 

           [159] => Array 
            (
             [id] => 159 
             [text] => Essen 
             [parent_id] => 155 
             [children] => Array 
              (
              )   
            )   
          )   
        ) 

       [156] => Array 
        (
         [id] => 156 
         [text] => RO 
         [parent_id] => 154 
         [children] => Array 
          (
           [160] => Array 
            (
             [id] => 160 
             [text] => Alba Iulia 
             [parent_id] => 156 
             [children] => Array 
              (
              )   
            ) 

           [161] => Array 
            (
             [id] => 161 
             [text] => Sibiu 
             [parent_id] => 156 
             [children] => Array 
              (
              )   
            )   
          )   
        )   
      )   
    ) 

私の再帰的な横断関数です。どんな助けもありがとうございます。ありがとう!

function traverseArray($array, &$in_arr = array()) 
{ 
    foreach($array as $k=>$v) 
    { 
     if($k == 'children' && empty($v)){ 
      // here i am querying children data for the end node that has ['children'] => array(). 
      // I did some tests and it seems that no matter what i do here does not affect the $in_array variable at all 

      $data = Location::all(array('conditions'=>array("parent_id=?", $array['id']))); 

      // here I try to assign the new childrens to $in_array 
      foreach ($data as $k=>$v){ 
       $in_arr['children'] = array('href'=>$v->id,'text'=>$v->name); 
      } 
     } 
     else{ 
      $in_arr[] = $v; 
     } 
     if(is_array($v)) 
     { 
      traverseArray($v); 
     } 
    } 
    return $in_arr; 
} 

答えて

0

下位レベルのオブジェクトを取得するために配列をトラバースし、そこから上に移動してその配列に要素を追加することができます。

$previousNode = null; 
$node = //Top Level Object 
while(sizeof($node->$children) > 0) 
{ 
    $prevNode = $node; 
    $node = $children[0]; 
} 

//Switch to up one level 
$node = previousNode; 

//Add children like 
array_push($node->$children, //element); 
0
/** 
* Add array into structure with some child id. 
* @param int $child_id Child id 
* @param array $structure Initial structure. 
* @param array $add Add child array. 
*/ 
function add_to_node($child_id, & $structure, $add) { 
    foreach($structure as & $data) { 
     if(isset($data['id'])) { 
      if($data['id'] == $child_id) { 
       $data['children'][ $add['id'] ] = $add; 
       break; 
      } 
     } 

     if(isset($data['children'])) { 
      add_to_node($child_id, $data['children'], $add); 
     } 
    } 
} 

// Initial array structure. 
$data = array(
    1 => array(
     'id' => 1, 
     'children' => array(
      12 => array(
       'id' => 12, 
       'children' => array(), 
      ), 

      15 => array(
       'id' => 15, 
       'children' => array(
        55 => array(
         'id' => 55, 
        ), 
       ), 
      ), 
     ), 
    ), 
); 

// This code will add new child with ID 44 to parent with id 55. 
add_to_node(55, $data, array(
    'id' => 44, 
    'text' => 'my text', 
)); 

print_r($data);