我已經查看了其他類似的問題,但找不到與我正在嘗試執行的操作相匹配的問題。合併/覆蓋一個數組中的值與另一個數值
我有2個數組,有不同的鍵和不同的長度。我需要用第二個值更新一個數組的值。第二個數組的長度是可變的,它沒有設置爲特定的數字。
$times = [
[ 'time' => '00:00', 'status' => 'unset' ],
[ 'time' => '01:00', 'status' => 'unset' ],
[ 'time' => '02:00', 'status' => 'unset' ],
[ 'time' => '03:00', 'status' => 'unset' ],
[ 'time' => '04:00', 'status' => 'unset' ],
[ 'time' => '05:00', 'status' => 'unset' ],
[ 'time' => '06:00', 'status' => 'unset' ],
[ 'time' => '07:00', 'status' => 'unset' ],
[ 'time' => '08:00', 'status' => 'unset' ],
[ 'time' => '09:00', 'status' => 'unset' ],
[ 'time' => '10:00', 'status' => 'unset' ],
[ 'time' => '11:00', 'status' => 'unset' ],
[ 'time' => '12:00', 'status' => 'unset' ],
[ 'time' => '13:00', 'status' => 'unset' ],
[ 'time' => '14:00', 'status' => 'unset' ],
[ 'time' => '15:00', 'status' => 'unset' ],
[ 'time' => '16:00', 'status' => 'unset' ],
[ 'time' => '17:00', 'status' => 'unset' ],
[ 'time' => '18:00', 'status' => 'unset' ],
[ 'time' => '19:00', 'status' => 'unset' ],
[ 'time' => '20:00', 'status' => 'unset' ],
[ 'time' => '21:00', 'status' => 'unset' ],
[ 'time' => '22:00', 'status' => 'unset' ],
[ 'time' => '23:00', 'status' => 'unset' ],
];
$times
是我需要更新的,$times
的值將總是具有(在一天24小時右)24的索引的數組。其他陣列$slots
會是這個樣子(我修剪下來的簡潔)
$slots = [
[
'id' => '104',
't_id' => '41',
'u_id' => null,
'status' => 'available',
'start_time' => '2017-08-01 12:00:00',
'end_time' => '2017-08-01 13:00:00',
'update_time' => '0000-00-00 00:00:00',
],
[
'id' => '105',
't_id' => '41',
'u_id' => null,
'status' => 'available',
'start_time' => '2017-08-01 15:00:00',
'end_time' => '2017-08-01 16:00:00',
'update_time' => '0000-00-00 00:00:00',
],
[
'id' => '106',
't_id' => '41',
'u_id' => null,
'status' => 'unavailable',
'start_time' => '2017-08-01 17:00:00',
'end_time' => '2017-08-01 18:00:00',
'update_time' => '0000-00-00 00:00:00',
],
];
行,所以我需要做的,就是更新$times
陣列與$slots
陣列的status
其中$slots
start_time
匹配$times
time
爲了獲得time
和start_time
匹配,我使用下面的代碼
foreach ($slots as $slot) {
$slot_time = new DateTime($slot[ 'start_time' ]);
$slot_time = $slot_time->format('H:i');}
呵呵,我基本上是自己寫的,但是因爲它不起作用而被刪除,結果我錯過了&,我給了這個接受的答案,因爲它比下面的答案稍快 – virtualLast