note 83007 added to function.shuffle

From: Date: Tue, 06 May 2008 16:42:45 +0000
Subject: note 83007 added to function.shuffle
Groups: php.notes 
Request: Send a blank email to php-notes+get-139096@lists.php.net to get a copy of this message
Another shuffle() implementation that preserves keys, does not use extra memory and perhaps is a bit easier to grasp. if (function_exists('shuffle_with_keys')===false) { function shuffle_with_keys(&$array) { /* Auxiliary array to hold the new order */ $aux = array(); /* We work with an array of the keys */ $keys = array_keys($array); /* We shuffle the keys */ shuffle($keys); /* We iterate thru' the new order of the keys */ foreach($keys as $key) { /* We insert the key, value pair in its new order */ $aux[$key] = $array[$key]; /* We remove the element from the old array to save memory */ unset($array[$key]); } /* The auxiliary array with the new order overwrites the old variable */ $array = $aux; } } ---- Server IP: 69.147.83.197 Probable Submitter: 200.62.165.42 ---- Manual Page -- http://www.php.net/manual/en/function.shuffle.php Edit -- https://master.php.net/note/edit/83007 Del: integrated -- https://master.php.net/note/delete/83007/integrated Del: useless -- https://master.php.net/note/delete/83007/useless Del: bad code -- https://master.php.net/note/delete/83007/bad+code Del: spam -- https://master.php.net/note/delete/83007/spam Del: non-english -- https://master.php.net/note/delete/83007/non-english Del: in docs -- https://master.php.net/note/delete/83007/in+docs Del: other reasons-- https://master.php.net/note/delete/83007 Reject -- https://master.php.net/note/reject/83007 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#139096) next »