Created
June 26, 2017 14:42
-
-
Save radutopala/06022fdb42abbbbf081ee255439b5c5a to your computer and use it in GitHub Desktop.
RecursiveArrayIterator example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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