Assuming I have an array like this:
array(10, 11, 12, 13, 14, 15)
I'm trying to make a way to get an array with the key-value pairs, but randomly without the pairs being equal. For example:
Valid
array(
10 => 14,
11 => 12,
12 => 15,
13 => 10,
14 => 11,
15 => 13
)
Not valid
array(
10 => 14,
11 => 11, // Não pode
12 => 15,
13 => 10,
14 => 12,
15 => 13
)
The second is not valid because the second position has the same value 11 in both the key and the value.
My attempt was this:
$result = array();
$array = array(10, 11, 12, 13, 14, 15);
$copy = array(10, 11, 12, 13, 14, 15);
foreach ($array as $a) {
$b = array_rand($copy, 1);
while (!in_array($copy[$b], $result)) {
if ($a != $copy[$b])
$result[$a] = $copy[$b];
else
$b = array_rand($copy, 1);
}
unset($copy[$b]);
}
It often works fine, but it has time that it does not go while , it reaches the maximum execution time.