2015-06-26 110 views
2

我想用父樹做一個很好的結構化類別,但我似乎無法找到正確的方法來正確地做到這一點。 這是首要的: 1產品有很多類別。 1類別可能有家長。PHP使多個數組嵌套數組

這是陣列我現在有(在這些陣列的值只是一個例子)

array:4[ 
    1=> array:2 [ 
    2 =>2 
    4 => 4 
    ] 
    2=> array:1[ 
    3=>3 
    ] 
    3=> array:1[ 
    5=>5 
    ] 
    9=> array:1[ 
    6=>6 
    ] 
] 

現在我想這個

array:4[ 
    1=> array:2 [ 
    2 => array:1 [ 
     3=> array:1 [ 
     5=>5 
     ] 
    ] 
    4 => 4 
    ] 
    9=> array:1[ 
    6=>6 
    ] 
] 

我怎麼轉換的第一到第二,而保持陣列鍵,我想用鍵來調用另一個陣列與所有相關的對象和數據 所以我可以得到這個結果: http://www.bootply.com/dFeKoQmgb2

+0

如果第一個數組包含一個鍵'5',則輸出數組應該更深一層,我正確嗎?如果是這種情況,您需要搜索遞歸。 :-) – Blaatpraat

+0

不完全是,這些數字只是與結構相關,這些數字將永遠不會相同。數組鍵是parent_id,值是兒童列表,我想將所有父母和孩子用相同的ID – aranna

回答

0

如果您嘗試以下代碼,該怎麼辦?請注意,在不改變遞歸內源數組的情況下,必須有更好的方法。

$array = array(
    1 => array(
     2 => 2, 
     4 => 4 
    ), 
    2 => array(
     3 => 3 
    ), 
    3 => array(
     5 => 5 
    ), 
    9 => array(
     6 => 6 
    ) 
); 

//we need the copy to delete there nodes that have been re-atached. Otherwise, we end up changing the array itself within recursion, which is not good 
$arrayCopy = $array; 

function recursive(&$array, &$arrayCopy) { 
    foreach($array as $key => &$value) { 
     //if we found the leaf node 
     if(!is_array($value)) { 
      // and there is something in the array copy with this index 
      if(array_key_exists($key, $arrayCopy)) { 
       // re-attach it to the initial array. $value is passed by reference. BTW, not a good approach as I stated above, but it works on the sample data 
       $value = $arrayCopy[$key]; 
       // remove that node from array copy in order to not double process it. 
       unset($arrayCopy[$key]); 
       // call it again for the leaf-node value 
       recursive($value, $arrayCopy); 
      } 
      // here is recursion exit, if we don't find anything in the arracy copy for the fiven leaf node 
     } else { 
      //recursive call it for the node 
      recursive($value, $arrayCopy); 
     } 
    } 
} 
// launch the recursion 
recursive($array, $arrayCopy); 

// now we need to remove the nodes that have been removed from the array copy, i.e. the ones being re-attached. We could not do this in the recursion 
$result = array(); 
foreach(array_keys($arrayCopy) as $initial_key) { 
    $result[$initial_key] = $array[$initial_key]; 
} 

var_dump($result); 
0

現在我固定它使用此功能,我敢肯定有一個更優雅的方式,因爲它只有當父母也是在陣列中的作品,這可是它的工作原理

array (size=5) 
    0 => 
    array (size=3) 
     'id' => int 2 
     'parent' => int 1 
     'name' => string 'voluptatem' (length=10) 
    1 => 
    array (size=3) 
     'id' => int 3 
     'parent' => int 2 
     'name' => string 'qui' (length=3) 
    2 => 
    array (size=3) 
     'id' => int 4 
     'parent' => int 1 
     'name' => string 'qui' (length=3) 
    3 => 
    array (size=3) 
     'id' => int 5 
     'parent' => int 3 
     'name' => string 'voluptate' (length=9) 
    4 => 
    array (size=3) 
     'id' => int 6 
     'parent' => int 9 
     'name' => string 'optio' (length=5) 


private function generatePageTree($datas, $parent=0, $limit=0) 
    { 
     if ($limit > 100) return ''; 
     $tree='<ul class="list-group">'; 
     foreach ($datas as $data) { 
      if ($data['parent'] == $parent) { 
       $tree.='<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="false"> 
          <div class="panel panel-default"> 
           <div class="panel-heading" role="tab" id="heading' . $data['id'] . '"> 
            <h4 class="panel-title" style="height:50px;"> 
             <a class="collapsed" role="button" data-toggle="collapse" data-parent="accordion" href="#collapse' . $data['id'] . '" aria-expanded="false" aria-controls="collapse' . $data['id'] . '"> 
              ' . $data['name'] . ' 
             </a> 
             <a href="' . action('[email protected]', $data['id']) . '" class="btn btn-default navbar-right"><i class="fa fa-search"></i>Toon</a> 
            </h4> 
           </div> 
           <div id="collapse' . $data['id'] . '" class="panel-collapse collapse out" role="tabpanel" aria-labelledby="heading' . $data['id'] . '"> 
            <div class="panel-body">'; 
       $tree.=$this->generatePageTree($datas, $data['id'], $limit ++); 
       $tree.='</div> 
           </div> 
          </div> 
          </div> 
         '; 
      } 
     } 
     $tree.='</ul>'; 

     return $tree; 
    }