Count how many times each element appeared with another in an array

Asked

Viewed 136 times

0

I have these arrays as reference:

$a_controle = array ('A','B','C','D','E','F');

var_dump($array_final) =>
array (size=5)
  0 => 
    array (size=3)
      0 => string 'A' (length=1)
      1 => string 'B' (length=1)
      2 => string 'C' (length=1)
  1 => 
    array (size=3)
      0 => string 'A' (length=1)
      1 => string 'B' (length=1)
      2 => string 'D' (length=1)
  2 => 
    array (size=3)
      0 => string 'A' (length=1)
      1 => string 'C' (length=1)
      2 => string 'E' (length=1)
  3 => 
    array (size=3)
      0 => string 'D' (length=1)
      1 => string 'E' (length=1)
      2 => string 'F' (length=1)
  4 => 
    array (size=3)
      0 => string 'A' (length=1)
      1 => string 'B' (length=1)
      2 => string 'P' (length=1)

How can I count how many times each element of $a_controle left along with another in $array_final?

For example:

  • The To appeared with B 3 times
  • The To appeared with C 2 times
  • The To appeared with D 1 times
  • The To appeared with And 1 time
  • The To appeared with F 0 times

The verification of To passes to the next element of $a_controle (element B):

  • The B appeared with C 1 time
  • The B appeared with D 1 time
  • The B appeared with And 0 time
  • The B appeared with F 0 time

The verification of B passes to the next element of $a_controle (element C).

The verification of C passes to the next element of $a_controle (element D).

The verification of D passes to the next element of $a_controle (element AND).

The verification of And passes to the next element of $a_controle (element F).

So successively for an array of unknown size. It is possible?

1 answer

1


You can use a foreach to get to the sub-array to be analyzed, and then use two for to check each element in their neighbors:

$a_controle = Array('A','B','C','D','E','F');
$array_final = Array(
    Array("A","B","C"),
    Array("A","B","D"),
    Array("A","C","E"),
    Array("D","E","F"),
    Array("A","B","P")
); 

$apareceu_com = Array();

foreach ($array_final as $sub_array_final){
    for ($i = 0; $i < count($sub_array_final); ++$i){
        for ($j = 0; $j < count($sub_array_final); ++$j){
            $elem1 = $sub_array_final[$i];
            $elem2 = $sub_array_final[$j];

            if ($i != $j){ //se não é o proprio elemento
                if (isset($apareceu_com[$elem1][$elem2])){
                    $apareceu_com[$elem1][$elem2]++;
                }
                else {
                    $apareceu_com[$elem1][$elem2] = 1;
                }
            }
        }
    }
}

print_r($apareceu_com);

Exit:

Array
(
[A] => Array
    (
        [B] => 3
        [C] => 2
        [D] => 1
        [E] => 1
        [P] => 1
    )

[B] => Array
    (
        [A] => 3
        [C] => 1
        [D] => 1
        [P] => 1
    )
...

Example in Ideone

  • But I didn’t understand this way out. Why is the letter D like 2? Would A come out with D 2 times? Because if it is incorrect.

  • @jsnow The letter D is as 2 because it came out 2 times. It is in the sub-root[1] as the last element and in the sub-root[3] as the first element. Writing otherwise $array_final[1][2] has D and $array_final[3][0] also has D

  • I don’t think you understand. I wonder how many times EACH element came out with ANOTHER using the $a_controle. The way I explained it upstairs. I need you to check the A with B, C, D, E, F. Then B with C, D, E, F. Then C with D, E, F and so on for each letter.

  • @jsnow I have also edited the answer, which I think I have now got what I wanted, so I would ask you to confirm

  • perfect, exactly what I intended. I will study your code to better understand what is happening there. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.