9

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;
$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).

4
  • Will the second array values always be two digits? Commented Feb 6, 2011 at 5:36
  • No, they are actually strings, I posted that as an example. Commented Feb 6, 2011 at 5:37
  • you might want to include the exact result you are after, to get the best answer. Commented Feb 6, 2011 at 5:40
  • 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). Commented Feb 6, 2011 at 5:45

6 Answers 6

18
$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.
Sign up to request clarification or add additional context in comments.

3 Comments

This doesn't work. The params are in the wrong order - array_multisort(array1,sorting order,sorting type,array2,array3...). Also the sorting order param value cannot be an array. php.net/manual/en/function.array-multisort.php
A very old answer that still kicking +1
@DanteCullari this is working correctly. Check in interpreter. It is working because sort order and flags are optional. With array_multisort you may sort any array count. Here you sort three arrays. Is just follow sorting of first "shuflled" array. And order here is just name - is array with shuffled indexes - is not "sort" as described in docs.
4

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];
}

Comments

0

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"
  }
}

Comments

0

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.

1 Comment

This does not work with duplicate values or non string/integer values in $array['one'].
0

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.

Comments

-1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.