Jump to content

How do I shuffle 2 arrays exactly the same?


roark

Recommended Posts

Hi Guys,

 

Could anyone help me with this problem?

I would like to shuffle 2 arrays the same i.e:

 

Before shuffling:

array 1: 1, 2, 3, 4, 5

array 2: a, b, c, d, e

 

After shuffling:

array 1: 2, 4, 5, 3, 1

array 2: b, d, e, c, a

 

Both arrays index order changed in the same manner.

I'm really stumped...

 

 

Thanks

shuffle() is random so there's no way you can repeat it. However you could implement you're own shuffle method, for example:

 

<?php

function shuffle2arrays($array1, $array2)
{
    if (count($array1) != count($array2))
    {
        return false;
    }

    $count = count($array1);
    $keys  = array_rand(range(0, ($count-1)), $count);

    $new_array1 = array();
    $new_array2 = array();

    for ($i = 0; $i < $count; $i++)
    {
        $new_array1[$i] = $array1[$keys[$i]];
        $new_array2[$i] = $array2[$keys[$i]];
    }

    return array($new_array1, $new_array2);
}

$array1 = array(1,2,3,4,5);
$array2 = array('a','b','c','d','e');

list($array1, $array2) = shuffle2arrays($array1, $array2);

print_r($array1);
print_r($array2);

?>

 

That's the only way I can think of doing it, someone else may have a much simpler solution though.

To make it more like shuffle() in-fact you could pass the array by reference:

 

function shuffle2arrays(&$array1, &$array2)
{
    if (count($array1) != count($array2))
    {
        return false;
    }

    $count = count($array1);
    $keys  = array_rand(range(0, ($count-1)), $count);

    $new_array1 = array();
    $new_array2 = array();

    for ($i = 0; $i < $count; $i++)
    {
        $new_array1[$i] = $array1[$keys[$i]];
        $new_array2[$i] = $array2[$keys[$i]];
    }

    $array1 = $new_array1;
    $array2 = $new_array2;

    return true;
}

 

Then you just need to call it with:

 

shuffle2arrays($some_array, $another_array);

You can do like this:

<?php
$arr1 = array(1,2,3,4,5,6);
$arr2 = array('a','b','c','d','e','f');

$shuffle = array();
for ($i = 0, $size = min(sizeof($arr1), sizeof($arr2)); $i < $size; ++$i) {
    $shuffle[] = array($arr1[$i], $arr2[$i]);
}

shuffle($shuffle);

foreach ($shuffle as $i => $v) {
    list($arr1[$i], $arr2[$i]) = $v; 
}

var_dump($arr1, $arr2);

 

Of course you need them to have equal length.

Alternatively, is there an issue with having a multidimensional array?

 

$foo[a][1]; ?

 

I dont know your exact implementation so I dont know if this would work...

 

It's not recursive if that's what you meant, but it'll work on any 2 single-dimension arrays with equal length. Actually I didn't consider this originally, but it'll only work properly on integer indexes - converting literal indexes to integer in the event of them.

 

I'd just go with Daniel's solution..

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.