PHP中从两个并行索引数组创建关联数组的最简单方法是什么?

时间:2021-02-02 12:12:40

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.

谢谢。

2 个解决方案

#1


21  

array_combine

array_combine

In your case:

在你的情况下:

$result_i_want = array_combine($a, $b);

#2


2  

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
)

#1


21  

array_combine

array_combine

In your case:

在你的情况下:

$result_i_want = array_combine($a, $b);

#2


2  

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
)