Last active
April 11, 2016 14:12
-
-
Save etiennemarais/bb33c166c4e84b8911ab382c143030e0 to your computer and use it in GitHub Desktop.
Collapses a multidimensional array into a simpler array that has the key as the nested dot notation along with it's value.
This file contains 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 | |
/** | |
* Link to original answer http://stackoverflow.com/a/10424516/461712 by http://stackoverflow.com/users/249538/goat | |
*/ | |
class ArrUtils { | |
/** | |
* @param $array | |
* @return array | |
*/ | |
protected function getArrayKeysDotNotation($array) | |
{ | |
$arrayIterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($array)); | |
$result = array(); | |
foreach ($arrayIterator as $leafValue) { | |
$keys = array(); | |
foreach (range(0, $arrayIterator->getDepth()) as $depth) { | |
$keys[] = $key = $arrayIterator->getSubIterator($depth)->key(); | |
} | |
$result[ join('.', $keys) ] = $leafValue; | |
} | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment