2011-12-28 62 views
4

孩子我有数组是这样的:让所有的深层多维数组

array(
    array(
     'id' => 1, 
     'children' => array(
      array(
       'id' => 2, 
       'parent_id' => 1 
      ), 
      array(
       'id' => 3, 
       'parent_id' => 1, 
       'children' => array(
        array(
         'id' => 4, 
         'parent_id' => 3 
        ) 
       ) 
      ) 
     ) 
    ) 
); 

数组去,如果有必要更深。我需要让任何特定ID的孩子。

谢谢。

回答

7
function getChildrenOf($ary, $id) 
{ 
    foreach ($ary as $el) 
    { 
    if ($el['id'] == $id) 
     return $el; 
    } 
    return FALSE; // use false to flag no result. 
} 

$children = getChildrenOf($myArray, 1); // $myArray is the array you provided. 

除非我失去了一些东西,遍历数组寻找的东西的id键,你正在寻找的ID匹配了(然后返回它的结果)。您也可以反复搜索(并给我一个第二张贴的代码,这将检查parentId键代替)...

-

递归版本,包括子元素:

function getChildrenFor($ary, $id) 
{ 
    $results = array(); 

    foreach ($ary as $el) 
    { 
    if ($el['parent_id'] == $id) 
    { 
     $results[] = $el; 
    } 
    if (count($el['children']) > 0 && ($children = getChildrenFor($el['children'], $id)) !== FALSE) 
    { 
     $results = array_merge($results, $children); 
    } 
    } 

    return count($results) > 0 ? $results : FALSE; 
} 

递归版本,不包括子元素

function getChildrenFor($ary, $id) 
{ 
    $results = array(); 

    foreach ($ary as $el) 
    { 
    if ($el['parent_id'] == $id) 
    { 
     $copy = $el; 
     unset($copy['children']); // remove child elements 
     $results[] = $copy; 
    } 
    if (count($el['children']) > 0 && ($children = getChildrenFor($el['children'], $id)) !== FALSE) 
    { 
     $results = array_merge($results, $children); 
    } 
    } 

    return count($results) > 0 ? $results : FALSE; 
} 
+0

它需要递归作为数组可以更深 – 2011-12-28 13:49:57

+0

它的工作只有顶级的元素,不适合儿童。 – cnkt 2011-12-28 13:50:11

+0

@Topener:问题在答案中改变了,所以我正在修复以适应。 - cnkt:在这工作,给我一分钟左右。 – 2011-12-28 13:51:38

0
function array_searchRecursive($needle, $haystack, $strict=false, $path=array()) 
{ 
    if(!is_array($haystack)) { 
     return false; 
    } 

    foreach($haystack as $key => $val) { 
     if(is_array($val) && $subPath = array_searchRecursive($needle, $val,  $strict, $path)) { 
      $path = array_merge($path, array($key), $subPath); 
      return $path; 
     } elseif((!$strict && $val == $needle) || ($strict && $val['id'] === $needle)) { 
      $path[] = $key; 
      return $path; 
     } 
    } 
    return false; 
} 

array_searchRecursive(5, $arr); 

- 参考:http://greengaloshes.cc/2007/04/recursive-multidimensional-array-search-in-php/

+0

你能帮我吗与此https://stackoverflow.com/questions/44804322/php-search-nested-array-of-array-and-return-only-matching-elements – Valay 2017-06-29 13:57:12

1

甲幼稚的方法将是由直到节点发现遍历树从根开始做一个详尽search on the tree。在最坏的情况下,你必须遍历整棵树,只记下你正在寻找的节点是最后一个节点,甚至不存在。

更好的方法是最初构建一个将ID映射到树内节点上的索引。有了这个,你只需要遍历整个树,然后通过索引直接访问节点。理想情况下,索引将在树形结构由平面数据构建时完成。

所以,如果你有一个平坦的阵列像your other question,你可以从它与平面阵列的只是一个迭代建树和指数双双:

// array to build the final hierarchy 
$tree = array(
    'children' => array() 
); 

// index array that references the inserted nodes 
$index = array(0=>&$tree); 

foreach ($arr as $key => $val) { 
    // pick the parent node inside the tree by using the index 
    $parent = &$index[$val['parent_id']]; 
    // append node to be inserted to the children array 
    $node = $val; 
    $parent['children'][$val['id']] = $node; 
    // insert/update reference to recently inserted node inside the tree 
    $index[$val['id']] = &$parent['children'][$val['id']]; 
} 

此代码是从my answer to a similar question拍摄。您发布的最终阵列位于$tree['children']。其中的每个节点都可以通过$index[12345]进行访问。

0

可以在代码中使用建立该

$iter = new RecursiveIteratorIterator(new RecursiveArrayIterator($array), RecursiveIteratorIterator::SELF_FIRST); 
foreach ($iter as $val) { 
    if (isset($val['id']) && $val['id'] === 3) { 
     print_r($val['children']); 
     break; 
    } 
}