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 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);
hakre
source share