note 107226 added to function.shuffle

From: Date: Tue, 17 Jan 2012 22:38:29 +0000
Subject: note 107226 added to function.shuffle
Groups: php.notes php.notes 
Request: Send a blank email to php-notes+get-185691@lists.php.net to get a copy of this message
I`ve tried to use shuffle_assoc(...) by Donald (on 15-Jun-2011 03:25), but is seems it works not correctly (PHP 5.3.1), at least with this array: $array = array(0=>0, 1=>1); it returns doubled array: Array ( [0] => 0 [1] => 1 [2] => 0 [3] => 1 ) instead of array with 2 values. I offer an alike function to shuffle an array and saving keys: <?php function shuffle_assoc($arr) { $ret = array(); if (!$arr) return $ret; $arr_count = count($arr); while (count($arr) > 0) { $key = array_rand($arr, 1); $ret[$key] = $arr[$key]; unset($arr[$key]); } return $ret; } ?> It`s not so cute, but it works for me. Best regards. ---- Server IP: 69.147.83.197 Probable Submitter: 213.151.9.177 ---- Manual Page -- http://www.php.net/manual/en/function.shuffle.php Edit -- https://master.php.net/note/edit/107226 Del: integrated -- https://master.php.net/note/delete/107226/integrated Del: useless -- https://master.php.net/note/delete/107226/useless Del: bad code -- https://master.php.net/note/delete/107226/bad+code Del: spam -- https://master.php.net/note/delete/107226/spam Del: non-english -- https://master.php.net/note/delete/107226/non-english Del: in docs -- https://master.php.net/note/delete/107226/in+docs Del: other reasons-- https://master.php.net/note/delete/107226 Reject -- https://master.php.net/note/reject/107226 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#185691) next »