Let’s say you have an array like this:
array
(
['one'] => array (
['wantedkey'] => 5
['otherkey1'] => 6
['otherkey2'] => 7
)
['two'] => => array (
['otherkey1'] => 5
['otherkey2'] => 6
['wantedkey'] => 7
)
['three'] => => array (
['otherkey1'] => 5
['wantedkey'] => 6
['otherkey2'] => 7
)
)
If you apply sth like this to the array you get all instances of ‘wantedkey’:
function test_print($item, $key) {
if ($key === 'wantedkey') {
print $item;
}
}
array_walk_recursive($myarray, 'test_print');
But array_walk_recursive doesn’t tell me (afaik) where I am in the array….is there any way to track the position where ‘wantedkey’ has been found in the example above with array_walk_recursive?
Any hint is highly appreciated, thanks in advance! 🙂
I know this has been asked before but I’m wondering if anyone has found a solution yet…
Use RecursiveArrayIterator if you need to track element position and hierarchy.