Reputation: 31
I want to merge two arrays where first array will be keys and second array will be values in result array.
$array1 = array('k1', 'k2');
$array2 = array('v1', 'v2');
output should be:
array(
'k1' => 'v1',
'k2' => 'v2',
)
Upvotes: 2
Views: 121
Reputation: 3698
array_combine() function in php is most simplest way to do this:
$array1 =array('k1','k2');
$array2 =array('v1','v2');
$result_array = array_combine ($array1, $array2);
Upvotes: 0
Reputation: 2272
You can use the array_combine
function. This function uses one array for keys, and one array for values.
You can use it as:
array_combine ( $keys, $values );
In your case it would be:
$array1 =array('k1','k2');
$array2 =array('v1','v2');
$combined_array = array_combine ( $array1, $array2 );
Upvotes: 0
Reputation: 5588
<?php
$a1=array("a","b","c","d");
$a2=array("Cat","Dog","Horse","Cow");
print_r(array_combine($a1,$a2));
?>
Upvotes: 1
Reputation: 45721
You use the built in array_combine
function
$keys = array('k1','k2');
$values = array('v1','v2');
$result = array_combine ($keys, $values);
Upvotes: 3