title: Grouping arrays in php and count data
body: <p>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.</p>
  
  <pre><code> Array #1
  
  Array
  (
    [0] =&gt; Array
      (
        [id] =&gt; 101
        [Name] =&gt; Superman
        [Franchise] =&gt; DC Comics
        [Superpower] =&gt; Inherent
      )
  
    [1] =&gt; Array
      (
        [id] =&gt; 908
        [Name] =&gt; Batman
        [Franchise] =&gt; DC Comics
        [Superpower] =&gt; Acquired
      )
  
    [2] =&gt; Array
      (
        [id] =&gt; 228
        [Name] =&gt; Wolverine
        [Franchise] =&gt; Marvel
        [Superpower] =&gt; Acquired
      )
  
    [3] =&gt; Array
      (
        [id] =&gt; 158
        [Name] =&gt; Iron Man
        [Franchise] =&gt; Marvel
        [Superpower] =&gt; Acquired
      )
  
    [4] =&gt; Array
      (
        [id] =&gt; 978
        [Name] =&gt; Thor
        [Franchise] =&gt; Marvel
        [Superpower] =&gt; Inherent
      )
  )
  </code></pre>
  
  <p>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'.</p>
  
  <pre><code>Array #2
  
  
  Array
  (
    [DC Comics] =&gt; Array
      (
        [Inherent] =&gt; 1
        [Acquired] =&gt; 1
      )
  
    [Marvel] =&gt; Array
      (
        [Inherent] =&gt; 1
        [Acquired] =&gt; 2
      )
  )
  </code></pre>