Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the most straightforward way in PHP to create an associative array from two parallel indexed arrays?

Tags:

arrays

php

Given the following two indexed arrays:

$a = array('a', 'b', 'c');
$b = array('red', 'blue', 'green');

What is the most straighforward/efficient way to produce the following associative array?:

$result_i_want = array('a' => 'red', 'b' => 'blue', 'c' => 'green');

Thanks.

like image 453
pjbeardsley Avatar asked Jun 08 '10 18:06

pjbeardsley


2 Answers

array_combine

In your case:

$result_i_want = array_combine($a, $b);
like image 78
Artefacto Avatar answered Nov 15 '22 20:11

Artefacto


This should do it:

$a = array('a', 'b', 'c');
$b = array('red', 'blue', 'green');
$c = array_combine($a, $b);
print_r($c);

Result:

Array
(
    [a] => red
    [b] => blue
    [c] => green
)
like image 36
Sarfraz Avatar answered Nov 15 '22 21:11

Sarfraz