Skip to content

Instantly share code, notes, and snippets.

@radutopala
Created June 26, 2017 14:42
Show Gist options
  • Save radutopala/06022fdb42abbbbf081ee255439b5c5a to your computer and use it in GitHub Desktop.
Save radutopala/06022fdb42abbbbf081ee255439b5c5a to your computer and use it in GitHub Desktop.
RecursiveArrayIterator example
<?php
$configs = [
'test' => [
'alfa' => 'beta'
],
'gamma' => 'first'
];
$iterator = new \RecursiveArrayIterator($configs);
$iteratorIterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::SELF_FIRST);
$config = [];
foreach ($iteratorIterator as $key => $value) {
if ($iteratorIterator->getDepth() == 0) {
unset($levelKey);
$levelKey[0] = "config_".$key;
}
if ($iteratorIterator->getDepth() > 0 && $iteratorIterator->callHasChildren()) {
$levelKey[$iteratorIterator->getDepth()] = $levelKey[$iteratorIterator->getDepth() - 1]."_".$key;
} else if ($iteratorIterator->getDepth() > 0 && !$iteratorIterator->callHasChildren()) {
$config[$levelKey[$iteratorIterator->getDepth()-1]."_".$key] = $value;
} else if ($iteratorIterator->getDepth() == 0 && !$iteratorIterator->callHasChildren()) {
$config[$levelKey[$iteratorIterator->getDepth()]] = $value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment