5

I'm trying to make a collection from some arrays of data:

$myCollection = collect(
    ['product_id' => 1, 'price' => 200, 'discount' => '50'],
    ['product_id' => 2, 'price' => 400, 'discount' => '50']
);

When I loop out I would like to do:

foreach ($myCollection as $product) {
    echo $product->price;
    echo $product->discount;
}

But the underlying elements appear to still be in an arrays format, how can I achieve the above output?

1
  • Did not any of the answers below solved your problem? Commented Dec 6, 2017 at 20:32

4 Answers 4

14

If you want the inner arrays to be a collection, then you can do so as follows:

$myCollection = collect([
    ['product_id' => 1, 'price' => 200, 'discount' => '50'],
    ['product_id' => 2, 'price' => 400, 'discount' => '50']
])->map(function($row) {
    return collect($row);
});

If you want the inner arrays to be objects, then you can do as follows:

$myCollection = collect([
    ['product_id' => 1, 'price' => 200, 'discount' => '50'],
    ['product_id' => 2, 'price' => 400, 'discount' => '50']
])->map(function($row) {
    return (object) $row;
});

You can also iterate over each of the results...

$myCollection = collect([
    ['product_id' => 1, 'price' => 200, 'discount' => '50'],
    ['product_id' => 2, 'price' => 400, 'discount' => '50']
])->map(function($row) {
    return (object) $row;
})->each(function($row) {
    echo sprintf('ProductId: %d, Price: %d, Discount: %s'.PHP_EOL, $row->product_id, $row->price, $row->discount);
});

Output:

ProductId: 1, Price: 200, Discount: 50
ProductId: 2, Price: 400, Discount: 50
Sign up to request clarification or add additional context in comments.

1 Comment

If I want to save this output into array $arr; I have attempted to use $arr[] = "Product Id: ……" but it does not allow me to do so….
5

Simple as you are getting a collection of an associative arrays because you are collecting arrays elements, so the collection helper doesn't want to modify it in case you need it as array.

Knowing this, if you want to collect objects you should pass an element of object type.

You can cast object data type into the array, like this:

$myCollection = collect( (object) array(
     (object)  ['product_id' => 1, 'price' => 200, 'discount' => '50'],
     (object)  ['product_id' => 2, 'price' => 400, 'discount' => '50'])
);

Then you have a collection of objects !

3 Comments

Thanks, but why would you use an object, why doesnt collect just work?
@panthro You are collecting a group of associative arrays elements, so the collection helper doesn't want to modify it in case you need it as associative array. In case you want to collect objects you should pass an element of object type :)
Ah this is the missing link i have been looking for!
0

Function collect() takes array of elements. You can fix your code also by enclosing all the elements into [] and then converting Collection elements to objects instead of leaving them to be arrays

    $myCollection = collect([
        (object)(['product_id' => 1, 'price' => 200, 'discount' => '50']),
        (object)(['product_id' => 2, 'price' => 400, 'discount' => '50'])
            ]
    );
    foreach ($myCollection as $product) {
        echo $product->price;
        echo $product->discount;
    }

1 Comment

Maybe you can help me. Look at this : stackoverflow.com/questions/51488623/…
-1

There's a syntax error in your collection definition:

$myCollection = collect([
    ['product_id' => 1, 'price' => 200, 'discount' => '50'],
    ['product_id' => 2, 'price' => 400, 'discount' => '50']
]);

Also, since you're using a Collection, you no longer need to use a foreach loop:

$myCollection->each(function ($item) {
    echo $item['price'];
})

7 Comments

Trying to get property of non-object
Not working: ` #message: "Trying to get property of non-object"`
Edit still fails to work. Property [price] does not exist on this collection instance
Don't think you need to define it as a collection of collections actually
Second edit returns to trying to get property of non-object
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.