I have 2 identical arrays with integers. What I’m trying to do is shuffle both arrays so no numbers are repeating and that they don’t match up. For instance:
If Array 1 has 4 ints {1, 2, 3, 4) and Array2 has 4 ints {1, 2, 3, 4). I need them to be shuffled so the elements in Array1 don’t match the elements in Array 2.
The code I have so far:
$Array1 = array();
$Array2 = array();
while($row = mysql_fetch_assoc($result))
{
$Array1[] = $row['id'];
$Array2[] = $row['id'];
}
shuffle($Array1);
shuffle($Array2);
array_unique($Array1);
array_unique($Array2);
for($i = 0; $i < sizeof($Array1); $i++)
{
if($Array1[$i] == $Array2[$i])
{
array_rand($Array1);
array_rand($Array2);
array_unique($Array1);
array_unique($Array2);
}
}
Without the array_rand() and array_unique() in the if statement, the output is correctly shuffled with no repeating numbers in both arrays, however I still get matches. Any help?
1 Answer