group array of php objects by object property
<p>I have a php object (book) with 3 properties: <code>name, category, description</code>
I then have a list of these <code>book</code> objects in an array.
I want to create a new associative array for these objects grouped by <code>category</code>.</p>

<p>Say I have 4 book objects in an array called <code>$books</code></p>

<pre><code>name    category  description
==================================
book1   cat1     this is book 1
book2   cat1     this is book 2
book3   cat2     this is book 3
book4   cat3     this is book 4
</code></pre>

<p>how can I create an associative array called <code>$categories</code></p>

<pre><code>$categories['cat1'] = array(book1, book2)
$categories['cat2'] = array(book2)
$categories['cat3'] = array(book3)
</code></pre>

<p>where book? is the book object and not the word</p>
