0

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
    )
)
4
  • 2
    Where's your code attempt? Commented Jun 10, 2017 at 4:21
  • 1
    Thor didn't inherit his superpower. He was given the Mjolnir. Commented Jun 10, 2017 at 4:25
  • what you want to count ?? array of value or array of same key?? Commented Jun 10, 2017 at 4:51
  • 1
    @Goose this is funny we are talking about code not thor Commented Jun 10, 2017 at 6:38

3 Answers 3

2

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);
Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly what i was looking for, simple and compact. Thank you..
1
  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

Comments

1

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
        )
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.