I need to combine the keys from an array, with the values in another array:
$a = array(4=>2000,5=>5000,7=>1000,3=>5000);
$b = arrray(array(0=>0,1=>4,2=>10,3=>1000),array()...)
This is what I’ve written (not working):
$keys = array_keys($a);
foreach ($b as $k => $v) {
array_combine($keys,$v);
}
What I expect to get is:
$c = array(array(4=>0,5=>4,7=>10,3=>1000),array(4=>...));
Since b has multiple arrays in it, you will just need to do a simple iteration of b:
You had the right idea with
array_keys($a)andarray_combine(). The other argument you needed to feet intoarray_combine()is the values,array_values($b). But since there are multiple arrays in b, you need to loop through each. As a failsafe in case there are more or less key/value pairs in a or any of the sub-arrays of b, I have addedarray_pad()to make sure that the two arrays passed in toarray_combine()are of equal length so that PHP doesn’t complain.