• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

多维数组层次结构

php 来源:sabrine chouay 4次浏览

我想知道如何通过标识符获取层次结构。多维数组层次结构

这是一个例子:

$inputArray = array(
     array(
      "text" => "Dir1", 
      "parent_id" => "", 
      "id" => "1", 
      "filesize" => "109" 
    ),array(
     "text" => "dir2", 
     "parent_id" => "", 
     "id" => "2", 
     "filesize" => "88", 
     "children" => array(
       "text" => "Dir3", 
       "parent_id" => "2", 
       "id" => "3", 
       "filesize" => "", 
       "children" => array(
        "text" => "dir4", 
        "parent_id" => "3", 
        "id" => "4", 
        "filesize" => "", 
        "children" => array(
         "text" => "dir5", 
         "parent_id" => "4", 
         "id" => "4", 
         "filesize" => "" 
        ) 
       ) 
     ) 
    )); 

寻找这个例子:

dir3/dir4/dir5 


===========解决方案如下:

function getText($array) { 
    $save[] = $array['text']; 
    if (isset($array['children'])) { 
    $save = array_merge($save, getText($array['children'])); 
    } 
    return $save; 
} 

foreach($inputArray as $x) { 
    echo implode('/', getText($x)) . "\n"; 
} 

结果

Dir1 
dir2/Dir3/dir4/dir5 

demo


版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)