BarryWalsh
BarryWalsh

Reputation: 1340

Laravel 4: Better way to create an array from an object than this

So I've retrieved an object from angular and need to convert it to an array.

The below works but I'm wondering if there's a cleaner or more efficient way to do this with Laravel:

$array = array();
foreach($input as $i){
    $array[] = $i['brand_name'];
}

Thanks!

Upvotes: 0

Views: 52

Answers (1)

Anam
Anam

Reputation: 12189

You can do it in one line:

$array = array_only($input, array('brand_name'));

if you need more than one value:

$array = array_only($input, array('brand_name', 'address'));

Upvotes: 1

Related Questions