This is my collection of php snippets that flatten arrays. There are scripts that work on traditional arrays and some that work on objects. I have tried to include where i got them from aswell.
1
// possibly from here - http://www.phpro.org/examples/Convert-Object-To-Array-With-PHP.html
/**
*
* Convert an object to an array
*
* @param object $object The object to convert
* @reeturn array
*
*/
function objectToArray( $object )
{
if( !is_object( $object ) && !is_array( $object ) )
{
return $object;
}
if( is_object( $object ) )
{
$object = get_object_vars( $object );
}
return array_map( 'objectToArray', $object );
}
/*** convert the array to object ***/
//$array = objectToArray( $obj );
/*** show the array ***/
//print_r( $array );
2
// not all of these flatteners can handle objects or associative arrays, experiment to find one that works
// i think associated arrays are different, and require different looping
// see - http://stackoverflow.com/questions/1319903/how-to-flatten-a-multidimensional-array
function flatten(array $array)
{
$return = array();
array_walk_recursive($array, function($a) use (&$return) { $return[] = $a; });
return $return;
}
// does not flatten array completely, same as above
function array_flatten($array)
{
$return = array();
foreach ($array as $key => $value) {
if (is_array($value)){ $return = array_merge($return, array_flatten($value));}
else {$return[$key] = $value;}
}
return $return;
}
3
// WORKS
// this works but murders all the keys and flattens them all to strings
// see - http://www.cowburn.info/2012/03/17/flattening-a-multidimensional-array-in-php/
// works on objects
function jon_flatten ($input)
{
$output = iterator_to_array(new RecursiveIteratorIterator(
new RecursiveArrayIterator($input)), FALSE);
return $output;
}
4
// see - http://stackoverflow.com/questions/526556/how-to-flatten-a-multi-dimensional-array-to-simple-one-in-php
function flatten_array($array, $preserve_keys = 0, &$out = array()) {
# Flatten a multidimensional array to one dimension, optionally preserving keys.
#
# $array - the array to flatten
# $preserve_keys - 0 (default) to not preserve keys, 1 to preserve string keys only, 2 to preserve all keys
# $out - internal use argument for recursion
foreach($array as $key => $child)
if(is_array($child))
$out = flatten_array($child, $preserve_keys, $out);
elseif($preserve_keys + is_string($key) > 1)
$out[$key] = $child;
else
$out[] = $child;
return $out;
}
5
// flatten multidimensional arrary into single dimension
// see - http://davidwalsh.name/flatten-nested-arrays-php
function array_flatten($array,$return)
{
for($x = 0; $x < count($array); $x++)
{
if(is_array($array[$x]))
{
$return = array_flatten($array[$x],$return);
}
else
{
if($array[$x])
{
$return[] = $array[$x];
}
}
}
return $return;
}
6
// flattenEachArrayToAnyDepth-------------------//
// see - http://davidwalsh.name/flatten-nested-arrays-php in the coments
//$res = array_flatten($myarray,array());
function array_flatten($array,$return){
foreach($array as $key => $value){
if(@is_array($value)){
$return = $this->array_flatten($value,$return);
}elseif(@$value){
$return[$key] = $value;
}
}
return $return;
}
7
// doesnt seem to work properely
// see - http://davidwalsh.name/flatten-nested-arrays-php#comment-56256}
/**
* Flattens a nested array.
*
* Based on:
* {@link http://davidwalsh.name/flatten-nested-arrays-php#comment-56256}
*
* @param array $array - The array to flatten.
* @param int $max_depth - How many levels to flatten. Negative numbers
* mean flatten all levels. Defaults to -1.
* @param int $_depth - The current depth level. Should be left alone.
*/
function array_flatten(array $array, $max_depth = -1, $_depth = 0) {
$result = array();
foreach ($array as $key => $value) {
if (is_array($value) && ($max_depth < 0 || $_depth < $max_depth)) {
$flat = array_flatten($value, $max_depth, $_depth + 1);
if (is_string($key)) {
$duplicate_keys = array_keys(array_intersect_key($array, $flat));
foreach ($duplicate_keys as $k) {
$flat["$key.$k"] = $flat[$k];
unset($flat[$k]);
}
}
$result = array_merge($result, $flat);
}
else {
if (is_string($key)) {
$result[$key] = $value;
}
else {
$result[] = $value;
}
}
}
return $result;
}
8
// WORKS
// this works but kills the keys
// see - http://davidwalsh.name/flatten-nested-arrays-php
function array_flatten($array, $return=array()) {
foreach ($array AS $key => $value) {
if(is_array($value))
{
$return = array_flatten($value,$return);
}
else
{
if($value)
{
$return[] = $value;
}
}
}
return $return;
}
9
// Ennio Wolsink
// @Manu: if you replace $return[] = $value; with $return[$key] = $value; you get the preserve the index names of the source array(s). Don’t know if that’s everyone preference, but that was what I was looking for. If one wanted to make this optional, he could add a 3rd optional parameter to this function to indicate wether or not to preserve index names, like so:
function array_flatten($array, $return=array(), $preserve_index_names = false)
{
foreach ($array AS $key => $value) {
if(is_array($value)) {
$return = array_flatten($value,$return, $preserve_index_names);
}
else {
if($value) {
if($preserve_index_names === false) {
$return[] = $value;
}
else {
$return[$key] = $value;
}
}
}
}
return $return;
}
And then run it like so to get a flattened array back with index names preserved:
$result = array_flatten($input, array(), true);
And like this if you don’t want the index names preserved:
$result = array_flatten($input);
10
// recursively reduces deep arrays to single-dimensional arrays
// $preserve_keys: (0=>never, 1=>strings, 2=>always)
// see - http://www.php.net//manual/en/function.array-values.php
function array_flatten($array, $preserve_keys = 1, &$newArray = Array()) {
foreach ($array as $key => $child) {
if (is_array($child)) {
$newArray =& array_flatten($child, $preserve_keys, $newArray);
} elseif ($preserve_keys + is_string($key) > 1) {
$newArray[$key] = $child;
} else {
$newArray[] = $child;
}
}
return $newArray;
}
11
// same array_flatten function, compressed and preserving keys.
// see - http://www.php.net//manual/en/function.array-values.php
function array_flatten($a,$f=array()){
if(!$a||!is_array($a))return '';
foreach($a as $k=>$v){
if(is_array($v))$f=self::array_flatten($v,$f);
else $f[$k]=$v;
}
return $f;
}