As you need to run the same function on each element of the $b array:
$a = array(4=>2000,5=>5000,7=>1000,3=>5000);
$b = array(array(0,4,10,1000), array(0,4,10,40));
you can make use of array_map with a callback function. The callback function then makes use of array_combine to assign your keys to the values.
As array_combine needs to have the keys to operate but the callback has only the values as input, I created a function that creates the actual callback function based on an array which keys will be taken for the array_combine operation.
As arrays can contain any values, some precautions are done. Empty arrays will not be processed at all, missing values for specific keys will be signalled as NULL:
$keyed = function($array)
{
$keys = array_keys($array);
// no keys, nothing to combine
if (!$len = count($keys)) {
return function($v) {return array();};
}
// default values are all NULL
$pad = array_fill(0, $len, NULL);
return function($values) use ($keys, $pad, $len)
{
// if input is not array, make it an empty array
!is_array($values) && $values = array();
return array_combine($keys, array_slice($values + $pad, 0, $len));
};
};
The $keyed is now an anonymous function that will return the callback function for array_map depending on it's input parameter for array keys:
$c = array_map($keyed($a), $b);
Anonymous functions are available in PHP since version 5.3.
Output:
array(2) {
[0]=> array(4) {
[4]=> int(0)
[5]=> int(4)
[7]=> int(10)
[3]=> int(1000)
}
[1]=> array(4) {
[4]=> int(0)
[5]=> int(4)
[7]=> int(10)
[3]=> int(40)
}
}