将两个数组组合成结果数组中的键和值? [重复]

问题描述:

This question already has an answer here:

I have two arrays, one with the keys and the other with the values:

key_array = ['key1','key2','key3']
values_array = ['val1','val2','val3']

How do I iterate through them (their size will vary) to obtain this:

array('key1' => 'val1',
      'key2' => 'val2',
      'key3' => 'val3')
</div>

此问题已经存在 这里有一个答案: p>

  • 一个数组的php值到另一个数组的键 3 答案 span> li> ul> div>

    我有两个数组,一个带键和 另一个有值: p>

      key_array = ['key1','key2','key3'] 
    values_array = ['val1','val2','val3'  ] 
      code>  pre> 
     
     

    如何迭代它们(它们的大小会有所不同)来获得: p>

      array(  'key1'=&gt;'val1',
    'key2'=&gt;'val2',
    'key3'=&gt;'val3')
      code>  pre> 
      div>

Use array_combine():

$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);

print_r($c);
?>


Array
(
    [green]  => avocado
    [red]    => apple
    [yellow] => banana
)