0

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"
    
          }
    
         }
Lucas Ln
  • 77
  • 1
  • 10
  • Can you show your attempt to solve the issue so we can help? Does order matter? Ex. `jungle, ball, car` Vs. `ball, jungle, car`? Will there always be 3 elements in each array? What should the output look like? – waterloomatt Jan 24 '22 at 19:19
  • see: https://stackoverflow.com/questions/13633954/how-do-i-count-occurrence-of-duplicate-items-in-array – Dirk J. Faber Jan 24 '22 at 19:30
  • doesnt matter the order , and there's some arrays with more than 3 elements – Lucas Ln Jan 24 '22 at 19:37

2 Answers2

1
$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
)
lukas.j
  • 6,453
  • 2
  • 5
  • 24
1
$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));
lukas.j
  • 6,453
  • 2
  • 5
  • 24
  • This answer is missing its educational explanation. Why is `array_filter()` necessary. How does this snippet work. Why is this the best approach you could think of? Please write something in addition to the code. – mickmackusa Oct 07 '22 at 10:53