Recursive function for flattening arrays
/**
* Flattens an array into a single dimension array
* @param array $array Array to flatten
* @param string $parent The parent indice
* @param string $separator The separator used in between indices
* @return array Flattened array
*/
function flatten($array, $parent = '', $separator = '.')
$return_array = array();
foreach($array as $key => $value) {
if( is_array($value) ) {
$return_array = array_merge($return_array, flatten($value, $parent.$separator.$key, $separator));
} else {
$return_array[$parent.$separator.$key] = $value;
}
}
return $return_array;
}
Written by Nikko Bautista
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Php
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#