note 86190 deleted from function.array-values by googleguy

From: Date: Tue, 22 Oct 2013 08:03:02 +0000
Subject: note 86190 deleted from function.array-values by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-196766@lists.php.net to get a copy of this message
Note Submitter: Anonymous ---- <?php $array=array( 'value1'=>'value', 'array1'=> array( 'subItem1'=>'subItem1', 'subItem2'=>'subItem2' ), 'array2'=> array( 'subItem1'=>'subItem1' )); $newArray=array(); array_flatten($array,$newArray); print_r( $newArray ); //Output //Array //( // [value1] => value // [array1|subItem1] => subItem1 // [array1|subItem2] => subItem2 // [array2|subItem1] => subItem1 //) // recursively reduces deep arrays to single-dimensional arrays function array_flatten($array, &$newArray = Array() ,$prefix='',$delimiter='|') { foreach ($array as $key => $child) { if (is_array($child)) { $newPrefix = $prefix.$key.$delimiter; $newArray =& array_flatten($child, $newArray ,$newPrefix, $delimiter); } else { $newArray[$prefix.$key] = $child; } } return $newArray; } ?>

« previous php.notes (#196766) next »