title: Array of categories from array of products
body: <p>I have a product array containing a list of products sorted by their display order.</p>
  
  <pre><code>$products = array(
  [0] = array(
   "productID" =&gt; 189736,
   "title" =&gt; "Spice Girls Album",
   "category" =&gt; "CD",
   "order" =&gt; "0"
  ),
  [1] = array(
   "productID" =&gt; 23087,
   "title" =&gt; "Snakes on a plane",
   "category" =&gt; "DVD",
   "order" =&gt; "0"
  ),
  [2] = array(
   "productID" =&gt; 9874,
   "title" =&gt; "The Beatles Album",
   "category" =&gt; "CD",
   "order" =&gt; "1"
  ), ... etc etc
  </code></pre>
  
  <p>I'm trying to figure out the logic of turning it into a category array like this:</p>
  
  <pre><code>$categories = array(
     [0] =&gt; array(
          "title" =&gt; "CD",
          "products" =&gt; array (
              [0] =&gt; "Spice Girls Album",
              [1] =&gt; "The Beatles Album"
          ) 
      ),
     [1] =&gt; array(
          "title" =&gt; "DVD",
          "products" =&gt; array (
              [0] =&gt; "Snakes on a plane"
          ) 
  )
  </code></pre>
  
  <p>So for each product I have:</p>
  
  <pre><code>if (!in_array($product['cateogry'], $categories)){
      $categories[] = $product['cateogry'];
      $categories[$product['category']][] = $product; 
  } else {
      $categories[$product['category']][];
  }
  </code></pre>
  
  <p>But this isn't working, because I don't think in_array is checking deep enough into the category array. Does anyone have any advice on the best way to solve this? Many thanks</p>