Skip to content

Instantly share code, notes, and snippets.

@Integralist
Last active July 7, 2023 03:38
Show Gist options
  • Save Integralist/11282503 to your computer and use it in GitHub Desktop.
Save Integralist/11282503 to your computer and use it in GitHub Desktop.
Loop recursively through a multi-level array using the SPL (Standard PHP Library) RecursiveArrayIterator
<?php
$multilevelArray = array(
"a" => array(
"i" => "aa",
"ii" => "bb",
"url" => "http://www.integralist.co.uk/",
"iii" => "cc"
),
"b" => "c",
"url" => "http://www.github.com/"
);
$counter = 0;
$array_obj = new RecursiveIteratorIterator(new RecursiveArrayIterator($multilevelArray));
foreach($array_obj as $key => $value) {
if ($key == 'url') {
$counter++;
}
}
echo $counter; // => 2
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment