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

184
Views
Create multidimensional array from multiple arrays in PHP

I have two arrays:

Array ( [0] => label [1] => data )
Array ( [0] => 1 [1] => 2 )

And I need merge them in a array like this:

Array ( [0] => Array ( [label] => 1 [data] => 2  ) )

I have tried:

for ($i=0; $i < count($inputs); $i++) {
    $new = array($cols[$i] => $inputs[$i]);
    $data[] = $new;
}

Any help is welcome ;)

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can simply use array_combine:

Creates an array by using one array for keys and another for its values

$arr1 = array(0 => 'label', 1 => 'data');
$arr2 = array(0 => 1, 1 => 2);
$arr3 = array_combine($arr1, $arr2);

print_r($arr3);

Result:

Array
(
    [label] => 1
    [data] => 2
)

Try it

over 4 years ago · Santiago Trujillo Report

0

You can do it like so if you want to use a loop

$array1 = ['label', 'data'];
$array2 = [1, 2];

$array_merged = [];

foreach($array1 as $key => $value) {
    $array_merged[$value] = $array2[$key];
}

var_dump($array_merged);

http://sandbox.onlinephpfunctions.com/code/c4e5bc71df53ebdafb0a54d43c3eadb4ea4cd241

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!