2016-09-19 71 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的总价值,而不是为每一个可能的登录。