我有这个例子:
$array['one'][0] = 0; $array['one'][1] = 1; $array['one'][2] = 2; $array['one'][3] = 3; $array['two'][0] = 00; $array['two'][1] = 11; $array['two'][2] = 22; $array['two'][3] = 33;
我怎样才能将它们混合起来以获得类似的东西:
$array['one'][0] = 2; $array['one'][1] = 1; $array['one'][2] = 3; $array['one'][3] = 0; $array['two'][0] = 22; $array['two'][1] = 11; $array['two'][2] = 33; $array['two'][3] = 00;
例如,我希望$array [‘one’] [0]和$array [‘two’] [0]得到洗牌以获得$array [‘one’] [x]和$array [‘two’] [ x](x是随机密钥,但两个阵列上的SAME).
像这样的东西可以工作.它类似于Mimikry的答案,除非你碰巧在数组1中有重复的值(这一个不使用数组1的值作为临时数组的键),否则它将起作用.
原文链接:https://www.f2er.com/php/138103.html假设两个阵列的大小相同.
$c = count($array['one']); $tmp = array(); for ($i=0; $i<$c; $i++) { $tmp[$i] = array($array['one'][$i],$array['two'][$i]); } shuffle($tmp); for ($i=0; $i<$c; $i++) { $array['one'][$i] = $tmp[$i][0]; $array['two'][$i] = $tmp[$i][1]; }