2016-09-19 68 views
0

我在谷歌上找到了一些解決方案,但它們沒有滿足我的要求。 我有一個數組,比較兩個數組並存儲在另一個數組中匹配和無與倫比的作爲0

$current_week = self::CurrentWeekDateRange($s_date, $e_date); 

它給我的結果是:

[0] => 2016-09-06 
    [1] => 2016-09-07 
    [2] => 2016-09-08 
    [3] => 2016-09-09 
    [4] => 2016-09-10 
    [5] => 2016-09-11 
    [6] => 2016-09-12 
    [7] => 2016-09-13 
    [8] => 2016-09-14 
    [9] => 2016-09-15 
    [10] => 2016-09-16 
    [11] => 2016-09-17 
    [12] => 2016-09-18 

現在我的下一個數組是這樣的:用戶登錄$返回一個陣列

[0] => Array 
     (
      [date_log] => 2016-09-08 
      [total] => 15 
     ) 

    [1] => Array 
     (
      [date_log] => 2016-09-13 
      [total] => 30 
     ) 

    [2] => Array 
     (
      [date_log] => 2016-09-14 
      [total] => 400 
     ) 

不,我有3個用戶表示它打印3次用戶日誌。 所以我想什麼是我想要date_log符合我上面的第一陣列,
如果它匹配它會給它會得到存儲在另一個數組,如果不匹配,那麼它將存儲0
我的問題我使用兩個環路螞蟻正在打印LOOP1 *循環2時代價值,但我想只有$ current_week時間值

我想是這樣的:

$current_week = self::CurrentWeekDateRange($s_date, $e_date); 

      $i = 0; 

      foreach ($current_week as $day){ 
       foreach ($returns as $return) { 
        if($day == $return['date_log']){ 
         $array_total_hours[$i]['total'] = $return['total']; 
         $array_total_hours[$i]['date_log'] = $return['date_log']; 
        } 
        else { 
         $array_total_hours[$i]['date_log'] = $return['date_log']; 
         $array_total_hours[$i]['total'] = 0; 
        } 

        $i++; 
       } 
      } 

print($array_total_hours); 

我想我的結果是這樣的:

[2016-09-06] => Array 
     (
      [date_log] => 2016-09-06 
      [total] => 0 
     ) 

    [2016-09-07] => Array 
     (
      [date_log] => 2016-09-07 
      [total] => 30 
     ) 

    [2016-09-08] => Array 
     (
      [date_log] => 2016-09-08 
      [total] => 400 
     ) 
    [2016-09-09] => Array 
     (
      [date_log] => 2016-09-09 
      [total] => 0 
     ) 
     . 
     . 
     . 
     . 
     . 
     . 
     . 
    [2016-09-18] => Array 
     (
      [date_log] => 2016-09-18 
      [total] => 0 
     ) 

回答

1

這一個應該做的工作。

$current_week = self::CurrentWeekDateRange($s_date, $e_date); 

$finalResult = array(); 
foreach ($current_week as $date) { 
    $finalResult[$date] = array('date_log' => $date, 'total' => 0); 
} 

$dates = array_keys($finalResult); 
// not sure where this one comes from 
$nextArray = array(
    array('date_log' => '2016-09-08', 'total' => 15), 
    array('date_log' => '2016-09-13', 'total' => 30) 
    ); 

foreach ($nextArray as $return) { 
    $record = array('date' => '', 'total' => 0); 
    if (in_array($return['date'], $dates)) { 
     $finalResult[$return['date']]['total'] = $return['total']; 
    } 
} 
+0

是的它工作完美; –

+1

thx ...我更換了一些線條更清晰 - 我想你也這樣做了。 – hummingBird

1

好您的解決方案時它的工作是這樣的:

$current_week = self::CurrentWeekDateRange($s_date, $e_date); 

     $i = 0; 

     foreach ($current_week as $day){ 
      $value = 0; 
      $date = $return 
      foreach ($returns as $return) { 
       if($day == $return['date_log']){ 
        $value = $return['total']; 
       break; 
       } 
      } 

      $array_total_hours[$i]['total'] = $value; 
      $array_total_hours[$i]['date_log'] = $date; 

      $i++; 

     } 

     print($array_total_hours); 

你只需要每$ current_week一次assing的總價值,而不是爲每一個可能的登錄。