Lucas Ln
Lucas Ln

Reputation: 77

Count how many times a value occurs with another value in a multidimensional array | PHP

So i have this multi-dimensional array , what i want to do is get how many times a value occurs with another.

For example:

Ball , Jungle and Car appears together 2 times , Ball and Car appears together 3 Times , Ball and Jungle appears together 3 Times , Ball and Sea appears together 2 Times .... Sea and Jungle appears together 1 time ....

one array may have 3 elements and the other may have 8 elements.

is it possible to acomplish that ?

 array(5) {
          [0]=>
          array(3) {
            [0]=>
            string(4) "Ball"
            [1]=>
            string(6) "Jungle"
            [2]=>
            string(3) "Sea"
            [3]=>
            string(3) "Fish"

          }
          [1]=>
          array(3) {
            [0]=>
            string(3) "Sea"
            [1]=>
            string(4) "Ball"
            [2]=>
            string(3) "Car"
          }
          [2]=>
          array(3) {
            [0]=>
            string(5) "Jungle"
            [1]=>
            string(4) "Ball"
            [2]=>
            string(3) "Car"
    
          }
       [3]=>
          array(3) {
            [0]=>
            string(5) "Jungle"
            [1]=>
            string(4) "Ball"
            [2]=>
            string(3) "Car"
    
          }
    
         }

Upvotes: 0

Views: 69

Answers (2)

lukas.j
lukas.j

Reputation: 7173

$items = [
    [ "Ball", "Jungle", "Sea" ],
    [ "Sea", "Ball", "Car", "Heaven", "Sky", "Beach", "Thunder", "Cave" ],
    [ "Jungle", "Ball", "Car", "More" ],
    [ "Jungle", "Sky", "Ball", "Car", "Thunder" ]
];

function combinations(array $items): array {
  $result = [ [] ];
  foreach ($items as $item) {
    foreach ($result as $values) {
      $elements = array_merge([ $item ], $values);
      sort($elements);
      $result[] = $elements;
    }
  }
  return $result;
}

function count_combinations(array $items): array {
  $data = [];
  foreach ($items as $item) {
    $data = array_merge($data, combinations($item));
  }
  $data = array_filter($data);
  $data = array_map(fn($value) => implode('–', $value), $data);
  sort($data);
  return array_count_values($data);
}

print_r(count_combinations($items));

Upvotes: 1

lukas.j
lukas.j

Reputation: 7173

$items = [
    [ "Ball", "Jungle", "Sea" ],
    [ "Sea", "Ball", "Car" ],
    [ "Jungle", "Ball", "Car" ],
    [ "Jungle", "Ball", "Car" ]
];

$data = [];

foreach ($items as $item) {

  sort($item);

  // 3
  $data[] = implode('–', $item);
  // 2
  $data[] = implode('–', [ $item[0], $item[1] ]);
  $data[] = implode('–', [ $item[0], $item[2] ]);
  $data[] = implode('–', [ $item[1], $item[2] ]);
  // 1
  $data[] = $item[0];
  $data[] = $item[1];
  $data[] = $item[2];

}

sort($data);

$result = array_count_values($data);

print_r($result);

This will print:

Array
(
    [Ball] => 4
    [Ball–Car] => 3
    [Ball–Car–Jungle] => 2
    [Ball–Car–Sea] => 1
    [Ball–Jungle] => 3
    [Ball–Jungle–Sea] => 1
    [Ball–Sea] => 2
    [Car] => 3
    [Car–Jungle] => 2
    [Car–Sea] => 1
    [Jungle] => 3
    [Jungle–Sea] => 1
    [Sea] => 2
)

Upvotes: 1

Related Questions