Reputation: 83
So I have the following two arrays which I try to merge together, the first one as $weekDays:
Array
(
[0] => Array
(
[id] => 1
[start] => 11:00:00
[end] => 17:00:00
)
[1] => Array
(
[id] => 2
[start] => 09:00:00
[end] => 17:00:00
)
[2] => Array
(
[id] => 3
[start] => 09:00:00
[end] => 17:00:00
)
[3] => Array
(
[id] => 5
[start] => 09:00:00
[end] => 17:00:00
)
[4] => Array
(
[id] => 6
[start] => 09:00:00
[end] => 17:00:00
)
[5] => Array
(
[id] => 7
[start] => 09:00:00
[end] => 17:00:00
)
[6] => Array
(
[id] => 8
[start] => 09:00:00
[end] => 17:00:00
)
)
And the second array as $all_slots:
Array
(
[0] => 0
[1] => 0
[2] => 2
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
)
Using this foreach, I try to loop each $weekDays item and merge it with an $all_slots item which has te same index number:
foreach($weekDays as $index => $day){
$slot = ['slot' => $all_slots[$index]];
array_merge_recursive($slot, $day);
}
But when I echo the updated $weekDays array it hasn't been merged at all, no errors either. It really does nothing for some reason, what am I missing here? I try to have the following result for the $weekDays array:
Array
(
[0] => Array
(
[id] => 1
[start] => 11:00:00
[end] => 17:00:00
[slot] => 0
)
[1] => Array
(
[id] => 2
[start] => 09:00:00
[end] => 17:00:00
[slot] => 0
)
[2] => Array
(
[id] => 3
[start] => 09:00:00
[end] => 17:00:00
[slot] => 2
)
[3] => Array
(
[id] => 5
[start] => 09:00:00
[end] => 17:00:00
[slot] => 0
)
[4] => Array
(
[id] => 6
[start] => 09:00:00
[end] => 17:00:00
[slot] => 0
)
[5] => Array
(
[id] => 7
[start] => 09:00:00
[end] => 17:00:00
[slot] => 0
)
[6] => Array
(
[id] => 8
[start] => 09:00:00
[end] => 17:00:00
[slot] => 0
)
)
Upvotes: 0
Views: 53
Reputation: 94662
array_merge_recursive()
returns a resulting array which you are not capturing.
A simpler approach would be
foreach($weekDays as $index => &$day){
$day['slot'] = $all_slots[$index];
}
you will have to make sure that the
$all_slots
array has the same or a greater number of occurances asweekDays
Upvotes: 2