I am trying to loop through an array, and return a key and child array of an array which has a set key => value.
For example...
Let's say I have
array(0 => array("chicken" => "free"), 1 => array("chicken" => "notfree"));
And I want to get the array array("chicken" => "notfree")
and know that the parent key is 1
I have the following...
function search($array, $key, $value) {
$arrIt = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
foreach($arrIt as $sub) {
$subArray = $arrIt->getSubIterator();
$subKey = $arrIt->key();
if(isset($subArray[$key]) && $subArray[$key] === $value) {
return array("key" => $subKey, "array" => iterator_to_array($subArray));
}
}
}
I can easily get the "chicken" => "notfree"
, but I can't seem to get the parent key, $arrIt->key()
keeps returning null? Any ideas?