PHP group multidimensional array : <p>I need help with grouping multidimensional PHP array. The array I have is :</p>

<pre><code>    Array
(
    [1385] =&gt; Array
        (
            [product_id] =&gt; 1385
            [product] =&gt; Tossed salad
            [category_ids] =&gt; Array
                (
                    [0] =&gt; 489
                )
        )
    [1386] =&gt; Array
        (
            [product_id] =&gt; 1386
            [product] =&gt; Green salad
            [category_ids] =&gt; Array
                (
                    [0] =&gt; 489
                )
        )
    [1387] =&gt; Array
        (
            [product_id] =&gt; 1387
            [product] =&gt; Milk Shake
            [category_ids] =&gt; Array
                (
                    [0] =&gt; 440
                )
        )
    [1388] =&gt; Array
        (
            [product_id] =&gt; 1388
            [product] =&gt; Mango Juice
            [category_ids] =&gt; Array
                (
                    [0] =&gt; 440
                )
        )
    [1389] =&gt; Array
        (
            [product_id] =&gt; 1389
            [product] =&gt; Orange Juice
            [category_ids] =&gt; Array
                (
                    [0] =&gt; 440
                )
        )
)
</code></pre>

<p>I want to group the array in different way so I can list them categories. Something like this : </p>

<pre><code>Array
    (
        [category_ids] =&gt; 489,
        [products] =&gt; 
          [0] =&gt; Array
            (
                [product_id] =&gt; 1385
                [product] =&gt; Tossed salad
            )
          [1] =&gt; Array
            (
                [product_id] =&gt; 1386
                [product] =&gt; Green salad
            )

        [category_ids] =&gt; 440,
        [products] =&gt; 
          [0] =&gt; Array
            (
                [product_id] =&gt; 1387
                [product] =&gt; Milk Shake
            )
          [1] =&gt; Array
            (
                [product_id] =&gt; 1388
                [product] =&gt; Mango Juice
            )
          [2] =&gt; Array
            (
                [product_id] =&gt; 1389
                [product] =&gt; Orange Juice
            )
    )
</code></pre>

<p>The structure can be wrong because I just made it with my text editor. But yes I want something like this. List those products under <code>category_ids</code>, sometimes there could be more then one <code>category_ids</code> as well. There many other products fields also, I shorten to make it look less complicated. There are <code>product_price</code>, <code>company_id</code> and some has multidimensional array like <code>product_options</code>.</p>
