I have two array as shown below:
$arr1 = ['monday','tuesday'];
$arr2 = [['1:00','3:00'],['12:00',4:00]];
I will like to make monday in $arr1
the key of the first array in $arr2
. SO my final result will be:
"monday" => ['1:00','3:00'],
"tuesday" => ['1:00','3:00'],
Thanks in advance.
CodePudding user response:
Here is a simple solution that will populate the $result
array with the required data:
$arr1 = ['monday','tuesday'];
$arr2 = [['1:00','3:00'],['12:00', '4:00']];
$result = [];
foreach ($arr1 as $index => $day) {
$result[$day] = $arr2[$index];
}
print_r($result);
CodePudding user response:
Use array_combine:
$arr3 = array_combine($arr1, $arr2);
It does exactly what you want to achieve.
CodePudding user response:
Or for larger data this should be faster:
$arr1 = ['monday','tuesday'];
$arr2 = [['1:00','3:00'],['12:00','4:00']];
$desired_output = array_map(
static function($key, $val) use ($arr2)
{
$new[$val] = $arr2[$key];
return $new;
},
array_keys($arr1), $arr1
);
var_dump($desired_output);