Reputation: 4539
I have an array built from an eloquent query which looks like this:
[['ctry' => 'US', 'subs' => 10], ['ctry' => 'AU', 'subs' => 25], ....]
And I want to convert it to:
['US' => 10, 'AU' => 25, ...]
I have tried array_map
, call_user_func_array
, laravel's flatten
without success. Thanks
Upvotes: 1
Views: 1698
Reputation: 1247
A possible solution can be
<?php
$sample_array = [['ctry' => 'US', 'subs' => 10], ['ctry' => 'AU', 'subs' => 25]]; # more elements in the array
$result_array = [];
foreach ($sample_array as $key => $value) {
array_push($result_array, [$value['ctry'] => $value['subs']]);
}
print_r($result_array);
Upvotes: 0
Reputation: 163948
If you're getting data from the model, you can use pluck()
method:
$model = Model::pluck('subs', 'ctry')->toArray();
Upvotes: 1