There's got to be a simple way to do what I need, but I just can't figure it out.
I have four different randomly generated variables that I need to "connect" and know their positions in a table. This image explains it better.
I've assigned each variable a 1 or a 2 and added them to an array ($var_array
) so I can know which match. For example, in the above table, 8n (var_array[0]
) could be assigned a value of 1. Then, the 11x, -15x, and 10x (var_array[1]
, var_array[2]
, var_array[3]
respectively) would be assigned a value of 2. So, print_r($var_array);
would give...
Array ( [0] => 1 [1] => 2 [2] => 2 [3] => 2 )
I need to then identify the "cell number" of matching elements. So, in this case, I would need to somehow identify cells 3, 5, and 7 as matching because I will use that later in the code. I will also need to extend this script to handle two different sets of matching elements.
How can I do this? Thank you.
EDIT: Here is the larger context of the code....
$variable_array = array('x', 'n', 'y', 'g');
$var_array = array();
do {
$num_a = mt_rand(-20, 20);
$num_b = mt_rand(-20, 20);
$num_c = mt_rand(-20, 20);
$num_d = mt_rand(-20, 20);
$var_a = mt_rand(1, 2);
$var_b = mt_rand(1, 2);
$var_c = mt_rand(1, 2);
$var_d = mt_rand(1, 2);
} while ($num_a == 0 || $num_b == 0 || $num_c == 0 || $num_d == 0 || (($var_a == $var_b) && ($var_a == $var_c) && ($var_a == $var_d) && ($var_b == $var_c) && ($var_c == $var_d) && ($var_b == $var_d)));
do {
$variable_1 = $variable_array[array_rand($variable_array)];
$variable_2 = $variable_array[array_rand($variable_array)];
} while ($variable_1 == $variable_2);
$var_array[] = $var_a;
$var_array[] = $var_b;
$var_array[] = $var_c;
$var_array[] = $var_d;