Support the ongoing development of Laravel.io →
1 year ago
Last updated 3 months 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();
1

Sign in to participate in this thread!

Cloudways

Your banner here too?

Moderators

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

Your logo here?

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

© 2022 Laravel.io - All rights reserved.