0

I am receiving an array of arrays, but would like each array to be an object.

I try this:

$objects->each(function($object) {
    return new Object($object);
});

but $objects is unchanged. I feel like based on the description of collection->each() and array_map, and this answer that this should work. But it clearly it doesn't, and a coworker thought I was an alien for thinking it should work.. so I must be missing something.

Is there a really efficient way like this for iterating through a collection and converting arrays to objects? And.. if this isn't what ->each does.. what does it do?

Community
  • 1
  • 1
Damon
  • 10,493
  • 16
  • 86
  • 144
  • What is the result of `dd($objects)` ? Please clarify what kind of collection is that and what the inner arrays contain. – The Alpha Aug 10 '14 at 01:49
  • pre `->each` it is `new Collection($arr)` where `$arr` is an array of object arrays.. ie `[['id'=>7,'name'=>'object7'], ['id'=>8,'name'=>'object8']]`. after the `->each` call it is unchanged – Damon Aug 10 '14 at 01:52

0 Answers0