note 75427 deleted from ref.array by levim

From: Date: Tue, 06 May 2014 22:10:43 +0000
Subject: note 75427 deleted from ref.array by levim
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-199470@lists.php.net to get a copy of this message
Note Submitter: sid dot pasquale at gmail dot com ---- <?php /* This function allow you to transform a multidimensional array in a simple monodimensional array. Usage: array_walk($oldarray, 'flatten_array', &$newarray); For example, this code below shows to you: Array ( [1] => Array ( [0] => 1 [1] => 2 ) [2] => Array ( [0] => 3 [1] => 4 ) ) Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) */ function flatten_array($value, $key, &$array) { if (!is_array($value)) array_push($array,$value); else array_walk($value, 'flatten_array', &$array); } $oldarray = array( 1 => array(1,2), 2 => array(3,4) ); $newarray = array(); array_walk($oldarray, 'flatten_array', &$newarray); echo "<pre>"; print_r($oldarray); print_r($newarray); echo "</pre>"; ?>

« previous php.notes (#199470) next »