Satyajyoti Biswas
Satyajyoti Biswas

Reputation: 927

Grouping arrays in php and count data

Recently I am working on a project that categorises 'Superheros' and 'Supervillains' based on their 'Franchaise' and origin of 'Superpower'. I want to fetch data from database as as Array #1 and displays them as Array #2 in php.

 Array #1

Array
(
  [0] => Array
    (
      [id] => 101
      [Name] => Superman
      [Franchise] => DC Comics
      [Superpower] => Inherent
    )

  [1] => Array
    (
      [id] => 908
      [Name] => Batman
      [Franchise] => DC Comics
      [Superpower] => Acquired
    )

  [2] => Array
    (
      [id] => 228
      [Name] => Wolverine
      [Franchise] => Marvel
      [Superpower] => Acquired
    )

  [3] => Array
    (
      [id] => 158
      [Name] => Iron Man
      [Franchise] => Marvel
      [Superpower] => Acquired
    )

  [4] => Array
    (
      [id] => 978
      [Name] => Thor
      [Franchise] => Marvel
      [Superpower] => Inherent
    )
)

Array #1 elements have to be grouped based on their 'Franchise' and count how many of them are 'Inherent' or 'Acquired' in terms of 'Superpower'.

Array #2


Array
(
  [DC Comics] => Array
    (
      [Inherent] => 1
      [Acquired] => 1
    )

  [Marvel] => Array
    (
      [Inherent] => 1
      [Acquired] => 2
    )
)

Upvotes: 0

Views: 70

Answers (3)

manian
manian

Reputation: 1438

Here is your code without much logic,

foreach ($array1 as $val) {
    if(!isset($array2[$val['Franchise']])) {
       $array2[$val['Franchise']] = array('Inherent' => 0, 'Acquired' => 0);
    }
    $array2[$val['Franchise']][$val['Superpower']]++;
}

print_r($array2);

Upvotes: 2

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

With single and short array_reduce:

// $arr is your initial array
$result = array_reduce($arr, function($r, $v){
    if (isset($r[$v["Franchise"]][$v["Superpower"]])) {
        $r[$v["Franchise"]][$v["Superpower"]] += $r[$v["Franchise"]][$v["Superpower"]];
    } else {
        $r[$v["Franchise"]][$v["Superpower"]] = 1;
    }
    return $r;
}, []);

print_r($result);

The output:

Array
(
    [DC Comics] => Array
        (
            [Inherent] => 1
            [Acquired] => 1
        )

    [Marvel] => Array
        (
            [Acquired] => 2
            [Inherent] => 1
        )
)

Upvotes: 1

Nidhi
Nidhi

Reputation: 1539

  1. array_column() function to aggregate an inner key from a 2D array.

  2. array_count_values() function counts all the values of an array.

Use this code snippet for count values:

$a = array ( array ("id" => "101", "Name" => "Superman", "Franchise" => "DC Comics", "Superpower" => "Inherent" ),
   array ( "id" => "908", "Name" => "Batman", "Franchise" => "DC Comics", "Superpower" => "Acquired" ),
   array ( "id" => "228", "Name" => "Wolverine", "Franchise" => "Marvel", "Superpower" => "Acquired" ),
   array ( "id" => "158", "Name" => "Iron Man", "Franchise" => "Marvel", "Superpower" => "Acquired" ),
   array ( "id" => "978", "Name" => "Thor", "Franchise" => "Marvel", "Superpower" => "Inherent" ));
  echo "<pre>";
$return = array();
// first group array values by franchise
foreach($a as $val) {
    if (!isset($return[$val['Franchise']])) {
        $return[$val['Franchise']] = array();
    }
    $return[$val['Franchise']][] = $val;
}
$arr = array();
// count elements by key value pair in particular franchise
foreach ($return as $key => $value) {
    $tmp = array_count_values(array_column($value, 'Superpower'));
    $arr[$key] = $tmp;
}
print_r($arr);

Demo is here

Upvotes: 1

Related Questions