比较两个数组和存储在另一个数组中匹配和不匹配为0

时间:2016-09-19 09:59:09

标签: php

我在谷歌上找到了一些解决方案但是他们没有达到我的要求。  我有一个数组,

$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

现在我的下一个数组是这样的:用户日志 $ return 数组

    [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 * loop2 倍值但我只想 $ 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
        )

2 个答案:

答案 0 :(得分:1)

okey使用您的解决方案它将如下工作:

$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分配一次总值,而不是每个可能的日志。

答案 1 :(得分: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'];
    }
}