Getting all the kids for a deep multi-dimensional array

I have an array like this:

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 ) ) ) ) ) ); 

The array goes deeper if necessary. I need to get kids for any id.

Thanks.

+4
source share
4 answers
 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. 

If I'm missing something, iterate over the array, looking for something that matches the id key and the identifier you are looking for (then return it as a result). You can also iteratively search (and give me a second code for this code that will check the parentId key) ...

-

The recursive version, includes elements for children:

 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; } 

Recursive version excluding child elements

 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; } 
+7
source

A naive approach would be to perform an exhaustive search on a tree by moving the tree starting at the root until a node is found. In the worst case, you need to iterate the whole tree just to note that the node you are looking for is the last node or even not existing.

A better approach would be to first create an index that maps identifiers to nodes within the tree. In this case, you will only need to go through the entire tree once, and then get direct access to the node through the index. Ideally, indexing will be performed while building the tree structure from flat data.

So, if you have a flat array, for example, in your other question , you can build both a tree and an index from it with only one iteration of a flat array:

 // 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']]; } 

This code is taken from my answer to a similar question . The final array that you placed is in $tree['children'] . Each node in it can be accessed using $index[12345] .

+1
source
 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 ); 

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

0
source

You can use assembly code for this

 $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; } } 
0
source

Source: https://habr.com/ru/post/1388304/


All Articles