Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
Laravel Array Format

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

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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

$model = Model::pluck('subs', 'ctry')->toArray();
over 4 years ago · Santiago Trujillo Report

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);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!