Skip to content

Instantly share code, notes, and snippets.

@gonssal
Last active September 21, 2019 01:38
Show Gist options
  • Save gonssal/bc49d34af5ae134dace16e3a44311be7 to your computer and use it in GitHub Desktop.
Save gonssal/bc49d34af5ae134dace16e3a44311be7 to your computer and use it in GitHub Desktop.
Using PHP's ArrayRecursiveIterator to find a key and its value in any multidimensional array.
<?php
$iterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator($multidimensional_array, RecursiveArrayIterator::CHILD_ARRAYS_ONLY),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iterator as $key => $value) {
if ($key === $search) {
$your_data = $value;
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment