array_column with an array of objects
<p>TLDR; My question is different from <a href="https://stackoverflow.com/questions/23335845/php-is-it-possible-to-use-array-column-with-an-array-of-objects">PHP. Is it possible to use array_column with an array of objects</a>. I want to only change the keys within the array and keep the objects, not having the objects' values stored in a separate array like the given answer.</p>

<p>I would like to set the keys, of an array with objects, to a value of the object. So this array:</p>

<pre><code>$array = Array
(
    [0] =&gt; stdClass Object
        (
            [id] = 12234
            [value] = some value
        )

    [1] =&gt; stdClass Object
        (
            [id] = 12994
            [value] = some value
        )

)
</code></pre>

<p>Should become:</p>

<pre><code>$array = Array
(
    [12234] =&gt; stdClass Object
        (
            [id] = 12234
            [value] = some value
        )

    [12994] =&gt; stdClass Object
        (
            [id] = 12994
            [value] = some value
        )

)
</code></pre>

<p>Now I could loop over the array, but I would prefer a more cleaner solution. I thought this should work:</p>

<pre><code>$newArray = array_column($array, null, 'id');
</code></pre>

<p>The only problem is I'm having an array of objects instead of an array of arrays and I'm not using PHP7 yet. Now I found a similar question  over here
<a href="https://stackoverflow.com/questions/23335845/php-is-it-possible-to-use-array-column-with-an-array-of-objects">PHP. Is it possible to use array_column with an array of objects</a></p>

<p>But the thing is it doesn't return what I expected. Cause this:</p>

<pre><code>$newArray = array_map(function($o) {
        return is_object($o) ? $o-&gt;id : $o['id'];
    }, $array);
</code></pre>

<p>Returns</p>

<pre><code>Array
(
    [0] =&gt; 12234
    [1] =&gt; 12994
)
</code></pre>

<p>Anyone who knows a clean solution (so without a for or foreach loop) for this?</p>