Skip to content

Instantly share code, notes, and snippets.

@etiennemarais
Last active April 11, 2016 14:12
Show Gist options
  • Save etiennemarais/bb33c166c4e84b8911ab382c143030e0 to your computer and use it in GitHub Desktop.
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.
<?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