PHP - recursive multidimensional matrix iterator

I am trying to write a recursive function of an array iterator in which the function will return the result set of all the sets given by '$ needle'. Where $ needle = key

Here is my function:

function recursive($needle, $array, $holder = array()) { foreach ($array as $key => $value) { if (gettype($value) == 'array') { if ($key != $needle) { recursive($needle, $value); } elseif ($key == $needle) { if (!empty($value)) { array_push($holder, $value); } } } } return $holder; } 

But I donโ€™t get all the results and instead get some empty results unless I specify !empty($value) , although the input array does not have empty sets. What am I doing wrong?

+8
arrays php recursion
source share
3 answers

You do not need to reinvent the wheel, as PHP has a standard Recursive Iterator API:

 //$array is your multi-dimensional array $result = []; $search = 'foo'; $iterator = new RecursiveIteratorIterator( new RecursiveArrayIterator( $array, RecursiveArrayIterator::CHILD_ARRAYS_ONLY ) ); foreach($iterator as $key=>$value) { if($search==$key && $value!=='') { $result[] = $value; } } 

-not that, since you are looking for a value by key - in general, $value will contain the entire subsection.

If you want to do this in your own recursive function, here is one:

 function recursive($needle, $array, $holder = []) { $holder = []; foreach($array as $key=>$value) { if($key===$needle && $value!=='') { $holder = array_merge($holder, [$value]); } if(is_array($value)) { $holder = array_merge($holder, recursive($needle, $value, $holder)); } } return $holder; } 
+16
source share

Perhaps finer control with true (tm) recursive array traversal through the RecursiveIterator interface and some key filters and array conversion functions is possible:

 $needle = '0'; $array = [[1]]; $it = new KeyFilter( new RecursiveIteratorIterator( new MyRecursiveArrayIterator($array) , RecursiveIteratorIterator::SELF_FIRST ) , $needle ); $result = iterator_to_array($it, FALSE); var_dump($result); 

Providing an approximate result as:

 array(2) { [0] => array(1) { [0] => int(1) } [1] => int(1) } 

Full Code Example ( Demo ):

 <?php /** * @link http://stackoverflow.com/q/19709410/367456 */ Class MyRecursiveArrayIterator extends ArrayIterator implements RecursiveIterator { public function hasChildren() { $current = $this->current(); return is_array($current) && count($current); } public function getChildren() { return new self($this->current()); } } class KeyFilter extends RegexIterator { public function __construct(Iterator $iterator, $key) { parent::__construct( $iterator, '/' . preg_quote($key) . '/', NULL, RegexIterator::USE_KEY ); } } $needle = '0'; $array = [[1]]; $it = new KeyFilter( new RecursiveIteratorIterator( new MyRecursiveArrayIterator($array) , RecursiveIteratorIterator::SELF_FIRST ) , $needle ); $result = iterator_to_array($it, FALSE); var_dump($result); 
+1
source share

A small modification to your design:

$holder = recursive($needle, $value, $holder);

Huh?

0
source share

All Articles