Collecting Column Values Into An Array
<p>One of the patterns that I frequently run across when developing is trying to collect a column/attribute value from a collection of objects into an array. For example:</p>

<pre><code>$ids = array();
foreach ($documents as $document) {
    $ids[] =  $document-&gt;name;
}
</code></pre>

<p>Am I the only one who runs into this? And does PHP have a way to solve this in fewer lines? I've looked but found nothing.</p>

<p>Since I use an MVC framework I have access to a BaseUtil class which contains common functions that don't really fit in any specific classes. One solution proposed by a co-worker is:</p>

<pre><code>class BaseUtil
{
    public static function collect($collection, $property) {
        $values = array();
        foreach ($collection as $item) {
            $values[] = $item-&gt;{$property};
        }
        return $values;
    }
}
</code></pre>

<p>Then I can just do:</p>

<pre><code>$ids = BaseUtil::collect($documents, 'name');
</code></pre>

<p>Not too shabby. Anyone else have any other ideas? And am I crazy or does this seem like a problem that PHP should have solved a long time ago?</p>
