PHP随机随机数组保持键=>值
我一直在寻找谷歌的答案,但似乎无法find一个傻瓜certificate,并不能真正负担得起(进入生产现场)。
我所拥有的是使用20多个filter的高级search,它返回包含ID和距离的数组。 我需要做的就是每次随机播放这些结果。 我现在出来的arrays是:
Array ( [0] => Array ( [id] => 1 [distance] => 1.95124994507577 ) [1] => Array ( [id] => 13 [distance] => 4.75358968511882 ) [2] => Array ( [id] => 7 [distance] => 33.2223233233323 ) [3] => Array ( [id] => 21 [distance] => 18.2155453552336 ) [4] => Array ( [id] => 102 [distance] = 221.2212587899658 ) )
我需要做的是每次都是随机的或者顺序的,但是保持id和距离对,即:
Array ( [4] => Array ( [id] => 102 [distance] = 221.2212587899658 ) [1] => Array ( [id] => 13 [distance] => 4.75358968511882 ) [3] => Array ( [id] => 21 [distance] => 18.2155453552336 ) [2] => Array ( [id] => 7 [distance] => 33.2223233233323 ) [0] => Array ( [id] => 1 [distance] => 1.95124994507577 ) )
谢谢 :)
shuffle
文档下的第一个用户post :
在保留键,值对的同时将随机数和非数组随机混合。 也返回混洗arrays,而不是洗牌。
function shuffle_assoc($list) { if (!is_array($list)) return $list; $keys = array_keys($list); shuffle($keys); $random = array(); foreach ($keys as $key) { $random[$key] = $list[$key]; } return $random; }
testing用例:
$arr = array(); $arr[] = array('id' => 5, 'foo' => 'hello'); $arr[] = array('id' => 7, 'foo' => 'byebye'); $arr[] = array('id' => 9, 'foo' => 'foo'); print_r(shuffle_assoc($arr)); print_r(shuffle_assoc($arr)); print_r(shuffle_assoc($arr));
从5.3.0开始,你可以这样做:
uksort($array, function() { return rand() > rand(); });
看看这个function:
$foo = array('A','B','C'); 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 */`enter code here` 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; } shuffle_with_keys($foo); var_dump($foo);
原帖在这里: http : //us3.php.net/manual/en/function.shuffle.php#83007
我提供的大部分答案都很难,所以我创build了这个小小的代码片段,把我的数组随机化,同时保留它们的键:
function assoc_array_shuffle($array) { $orig = array_flip($array); shuffle($array); foreach($array AS $key=>$n) { $data[$n] = $orig[$n]; } return array_flip($data); }
function shuffle_assoc($array) { $keys = array_keys($array); shuffle($keys); return array_merge(array_flip($keys), $array); }
尝试从这里使用fisher-yatesalgorithm:
function shuffle_me($shuffle_me) { $randomized_keys = array_rand($shuffle_me, count($shuffle_me)); foreach($randomized_keys as $current_key) { $shuffled_me[$current_key] = $shuffle_me[$current_key]; } return $shuffled_me; }
我必须为我的本科高级论文实施类似的东西,而且工作得很好。
Charles Iliya Krempeaux在这个问题上有一个很好的写作和一个非常适合我的function:
function shuffle_assoc($array) { // Initialize $shuffled_array = array(); // Get array's keys and shuffle them. $shuffled_keys = array_keys($array); shuffle($shuffled_keys); // Create same array, but in shuffled order. foreach ( $shuffled_keys AS $shuffled_key ) { $shuffled_array[ $shuffled_key ] = $array[ $shuffled_key ]; } // foreach // Return return $shuffled_array; }
我试了最多的投票解决scheme没有受欢迎的洗牌名单。 这是我做出的改变。 我想我的数组键从1开始。
$list = array_combine(range(1,10),range(100,110)); $shuffle_list = shuffle_assoc($list); function shuffle_assoc($list) { if (!is_array($list)) return $list; $keys = array_keys($list); shuffle($list); $random = array(); foreach ($keys as $k => $key) { $random[$key] = $list[$k]; } return $random; }