note 96282 deleted from function.shuffle by danbrown

From: Date: Thu, 18 Feb 2010 14:53:06 +0000
Subject: note 96282 deleted from function.shuffle by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-166150@lists.php.net to get a copy of this message
Note Submitter: anon ---- an extension of some previous notes to shuffle two-dimensional arrays: -preserves main arrays keys -shuffles every internal array the same <?php function array_multishuffle($array = array()){ $key = key($array); $count = count($array[$key]); $keys = range(0,$count-1); shuffle($keys); $new_array = array(); while($sub_array = current($array)){ $name = key($array); foreach($keys as $key){ $new_array[$name][] = $sub_array[$key]; } next($array); } return $new_array; } $array = array( 'id'=>array(10,20,30,40), 'name'=>array('bob','sam','ken','hermenegildo') ); print_r(array_multishuffle($array)); /* Array ( [id] => Array ( [0] => 20 [1] => 40 [2] => 10 [3] => 30 ) [name] => Array ( [0] => sam [1] => hermenegildo [2] => bob [3] => ken ) )*/ ?>

« previous php.notes (#166150) next »