2017-08-02 71 views
1

我已經查看了其他類似的問題,但找不到與我正在嘗試執行的操作相匹配的問題。合併/覆蓋一個數組中的值與另一個數值

我有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其中$slotsstart_time匹配$timestime

爲了獲得timestart_time匹配,我使用下面的代碼

foreach ($slots as $slot) { 
      $slot_time = new DateTime($slot[ 'start_time' ]); 
      $slot_time = $slot_time->format('H:i');} 

回答

0

您將此代碼添加到您的最後foreach
的符號&讓數組的值$t可修改

foreach ($slots as $slot) { 
      $slot_time = new DateTime($slot[ 'start_time' ]); 
      $slot_time = $slot_time->format('H:i'); 

      foreach ($times as &$t) 
       if($t['time']==$slot_time){ 
        $t['status']=$slot['status']; 
       } 
     } 

和這裏的execution的輸出,其中我已經使用print_r($times);

+0

呵呵,我基本上是自己寫的,但是因爲它不起作用而被刪除,結果我錯過了&,我給了這個接受的答案,因爲它比下面的答案稍快 – virtualLast

0

你是正確的方向。你可以做以下事情;

$times_time = array_column($times, "time"); //only contain `time` values 

foreach ($slots as $slot) { 
    $slot_time = new DateTime($slot[ 'start_time' ]); 
    $slot_time = $slot_time->format('H:i'); 
    $search_key = array_search($slot_time,$times_time);  
    if($search_key>0){ //search key 
     $times[$search_key]['status']=$slot[ 'status' ]; 
    } 
} 
print_r($times); 

Demo

+0

感謝您的回答,真的很喜歡它,並讚賞演示,上面的答案稍快,這就是爲什麼我給了它接受的答案 – virtualLast

+0

嗯它你真的注意到並檢查輸出時間。我的建議會稍微快一點,因爲它可以消除循環中不需要的循環。 @virtualLast –

+0

[eval](https://eval.in/840562) [eval2](https://eval.in/840560) 我看錯了嗎?使用PHP 7,我應該看哪個值? – virtualLast

0
<html> 
<head> 
<title>Online PHP Script Execution</title> 
</head> 
<body> 
<?php 


$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' ], 
    ]; 

$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', 
     ], 
    ]; 

    $param = array(); 

    for($i=0;$i<count($times);$i++){ 
     for($j=0;$j<count($slots);$j++){ 
      if($times[$i]['time']==date('H:i',strtotime($slots[$j]['start_time']))){ 
       $times[$i]['start_time'] = date('H:i',strtotime($slots[$j]['start_time'])); 
       $param[$i]['time']=$times[$i]['time']; 
       $param[$i]['status'] = $times[$i]['status']; 
       $param[$i]['start_time'] = $times[$i]['start_time']; 
      } 

     } 
    } 
    print_r($param); 
?> 

</body> 
</html> 
相關問題