title: Reduce an array of objects in PHP
body: <p>I have an array of objects. Something like this:</p>
  
  <pre><code>$my_array = [
      (object) ['name' =&gt; 'name 0'],
      (object) ['name' =&gt; 'name 1'],
      (object) ['name' =&gt; 'name 2'],
  ];
  </code></pre>
  
  <p>And I'd like to reduce it to a concatenation of all <code>name</code> properties like:</p>
  
  <blockquote>
    <p>name 0 / name 1 / name 2</p>
  </blockquote>
  
  <p>One way would be:</p>
  
  <pre><code>$result = [];
  foreach ($my_array as $item) {
      $result[] = $item-&gt;name;
  }
  echo implode(' / ', $result);
  </code></pre>
  
  <p>But I'd prefer something more <em>compact</em>, like using <code>array_map</code>:</p>
  
  <pre><code>implode(' / ', array_map(function($item) {
      return $item-&gt;name; 
  }, $my_array ));
  </code></pre>
  
  <p>Given that in fact I want to <em>reduce</em> and array to a string I thought it would be cleaner with <code>array_reduce</code> but the only solution I can come out with is:</p>
  
  <pre><code>array_reduce($my_array, function($carry, $obj) {
      return empty($carry) ? $obj-&gt;name : $carry .= " / $obj-&gt;name"; 
  });
  </code></pre>
  
  <p>Yet it doesn't <em>feel</em> cleaner... So the question is simple:</p>
  
  <p>Can anybody think of an <strong>understandable</strong> better/cleaner solution?</p>