1

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

2 Answers 2

1

If you're getting data from the model, you can use pluck() method:

$model = Model::pluck('subs', 'ctry')->toArray();
Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

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.