Skip to content

Instantly share code, notes, and snippets.

@etiennemarais

etiennemarais/example.php

Last active Apr 11, 2016
Embed
What would you like to do?
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