Support the ongoing development of Laravel.io →
posted 2 years ago
Last updated 1 year ago.
0
moderator Solution

You can do that with the keyBy function: https://laravel.com/docs/8.x/collections#method-keyby

$cars =  [
    ['Item' => "Volvo", 'Price' => 22, 'NewPrice' => 18],
    ['Item' => "Volvo", 'Price' => 22, 'NewPrice' => 18.56],
    ['Item' => "BMW",  'Price' => 23, 'NewPrice' => 5.09],
    ['Item' => "Saab",  'Price' => 90, 'NewPrice' => 23],
    ['Item' => "Saab",  'Price' => 90, 'NewPrice' => 24.90],
    ['Item' => "Saab",  'Price' => 90, 'NewPrice' => 13.33],
];

$collection = new \Illuminate\Support\Collection($cars);

$cars = $collection->keyBy('Item');
// Or if you don't want the Item as key of the result.
$cars = $collection->keyBy('Item')->values();

claw-hammer liked this reply

1

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.