Shuffle 2 php arrays in the same way
I have this for example:
$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;
How can I shuffle them both to get something like:
$array['one'][0] = 2;
$array['one'][1] = 1;
$array['one'][2] = 3;
$array['one'][3] = 0;
$array['two'][0] = 22;
$array['two'][1] = 11;开发者_StackOverflow中文版
$array['two'][2] = 33;
$array['two'][3] = 00;
Or any other random order, but having the same "random factor" in both?
For example, I want that $array['one'][0]
and $array['two'][0]
get shuffled to get $array['one'][x]
and $array['two'][x]
(x
being a random key, but the SAME on both arrays).
$count = count($array['one']);
$order = range(1, $count);
shuffle($order);
array_multisort($order, $array['one'], $array['two']);
- Works with arrays with elements of any type (objects and arrays too).
- This way may by used with any number of arrays (not only two).
- Works with duplicated values.
- Clean code.
Something like this could work. It's similar to Mimikry's answer except this one will work even if you happen to have duplicate values in array one (this one doesn't use values of array one as keys of the temporary array).
Assuming both arrays are of the same size.
$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];
}
hopefully i get you right.
For your example above, this code should work. But it's pretty hacky...
$array['one'][0] = 'A';
$array['one'][1] = 'B';
$array['one'][2] = 'C';
$array['one'][3] = 'D';
$array['two'][0] = 'AA';
$array['two'][1] = 'BB';
$array['two'][2] = 'CC';
$array['two'][3] = 'DD';
// save the dependencies in tmp array
foreach ($array['two'] as $key => $val) {
$tmp[$array['one'][$key]] = $val;
}
shuffle($array['one']);
// restore dependencies in tmp2 array
foreach ($array['one'] as $key => $val) {
$tmp2[$key] = $tmp[$val];
}
// overwrite with restore array
$array['two'] = $tmp2;
var_dump($array):
array(2) {
["one"]=>
array(4) {
[0]=>
string(1) "B"
[1]=>
string(1) "A"
[2]=>
string(1) "C"
[3]=>
string(1) "D"
}
["two"]=>
array(4) {
[0]=>
string(2) "BB"
[1]=>
string(2) "AA"
[2]=>
string(2) "CC"
[3]=>
string(2) "DD"
}
}
The best practice, imho, is as follows:
$tmp = array_combine($array['one'],$array['two']);
shuffle($tmp);
$array['one'] = array_keys($tmp);
$array['two'] = array_values($tmp);
This is clear code and should be fast. No need to reinvent the wheel like in some other answers.
This isn't an example with multidimensional arrays but it works great for sorting multiple normal arrays the same way and with shuffle. Maybe it could be adapted for multidimensional arrays too. It does assume all arrays are the same length.
$array_count = count($array_1);
for($i=0;$i<=($array_count-1);$i++){
$temp_array[$i] = $i;
}
shuffle($temp_array);
for($i=0;$i<=($array_count-1);$i++){
$o = $temp_array[$i];
$array_1_sorted[$i]=$array_1[$o];
$array_2_sorted[$i]=$array_2[$o];
$array_3_sorted[$i]=$array_3[$o];
}
This will give you new arrays which are all sorted the same random way. You could then set the old arrays equal to the new ones or keep them in-tact.
Not sure exactly what you are trying to do, but your best bet is probably to put array 1 and array 2 into a multi-dimensional array and then random the primary array. That will give you a random arrangement of values, but within each key will be the values of each array. We could give you more specific answers if you can provide a bit more detail of exactly what you are doing.
精彩评论