I have two arrays which i want to merge but following certain conditions.
$wt = [
['time' =>'07:00'],
['time' =>'07:30'],
['time' =>'08:00'],
['time' =>'08:30'],
['time' =>'09:00'],
['time' =>'09:30'],
];
$tasks = [
['time' => '07:30', 'name' => 'john'],
['time' => '08:00', 'name' => 'fred'],
];
The desired result should look like this:
$full = [
['time' => '07:00', 'name' => null],
['time' => '07:30', 'name' => 'john'],
['time' => '08:00', 'name' => 'fred'],
['time' => '08:30', 'name' => null],
['time' => '09:00', 'name' => null],
['time' => '09:30', 'name' => null],
];
I'm looping, but I'm making a mistake somewhere.
$full = []; // this is the merge array
foreach ($wt as $k => $item) {
$full[$k]['time'] = $item['time'];
foreach ($tasks as $task) {
if($item['time'] == $task['time']) {
$full[$k]['name'] = $task['name'];
} else {
$full[$k]['name'] = null;
}
}
}
Here is my wrong result...
Array
(
[0] => Array
(
[time] => 07:00
[name] =>
)
[1] => Array
(
[time] => 07:30
[name] =>
)
[2] => Array
(
[time] => 08:00
[name] => fred
)
[3] => Array
(
[time] => 08:30
[name] =>
)
[4] => Array
(
[time] => 09:00
[name] =>
)
[5] => Array
(
[time] => 09:30
[name] =>
)
)
I'm losing 'john'
It should be done so that the first array is detected with the second. So, by matching time from the first one, fill the array 'full' with the name. Otherwise, it should be left blank.
Can anyone help? Thank you.