0

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.

Table image

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;
gtilflm
  • 1,389
  • 1
  • 21
  • 51
  • 1
    have you considered creating a 2-dimensional array to hold value along with cell position ? – Maximus2012 Aug 05 '13 at 19:42
  • How are you storing the table? – Mike Aug 05 '13 at 19:47
  • [Take a look at this please](http://stackoverflow.com/questions/3145607/php-check-if-an-array-has-duplicates) – ODelibalta Aug 05 '13 at 19:47
  • 2
    @Sage how does that help? – Mike Aug 05 '13 at 19:48
  • You said you need to identify the cells 3,5 and 7 as matching because in your array, they have the same value. Given how fast you replied to my comment, it is not humanly possible to read the link I posted. If you actually read it, you can see that the matching values are found in the loop. You can also get the key information from that loop with a quick edit to it. – ODelibalta Aug 05 '13 at 19:51
  • @gtilflm Could you explain what makes 8n=>1 and 11x/-15x/10x => 2 ? Perhaps I could help you more if I better understood what you're trying to do.. – asifrc Aug 05 '13 at 19:59
  • @asifrc: It actually comes from the variable (like in math, not PHP) that is being generated randomly. I've updated my OP with a larger piece of code. – gtilflm Aug 05 '13 at 20:05
  • @Mike: I don't know what you mean by "storing" the table. I will just be echoing it out to the browser later on. – gtilflm Aug 05 '13 at 20:06
  • Where are the "cells" that you are trying to match to? – asifrc Aug 05 '13 at 20:14
  • @asifrc: Later in the code, I will use the matching "cell numbers" as y-coordinates. So, like in my original post, I would need to know that cells 3, 5, and 7 match and I would then need to set y-coordinates of "3", "5", and "7". Does that make sense? – gtilflm Aug 05 '13 at 20:19

1 Answers1

0
$arr = array(1, 2, 2, 2);
$matching_cells = array();
foreach ($arr as $key => $value){
        $matching_cells[$value][] = $key*2 + 1;
}   
print_r($matching_cells);

Output:

Array
(
    [1] => Array
        (
            [0] => 1
        )

    [2] => Array
        (
            [0] => 3
            [1] => 5
            [2] => 7
        )

)
Expedito
  • 7,771
  • 5
  • 30
  • 43