Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 比较两个数组并存储在另一个数组中,匹配和不匹配为0_Php - Fatal编程技术网

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

Php 比较两个数组并存储在另一个数组中,匹配和不匹配为0,php,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

我在谷歌上找到了一些解决方案,但它们不能满足我的要求。 我有一个数组

$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
现在我的下一个数组是这样的:user log$返回一个数组

不,我有,3个用户意味着它打印3次用户日志。 所以我想要的是将date_log匹配到我的第一个数组上面, 如果其匹配,则它将在另一个数组中获得存储,如果不匹配,则它将存储0。 我的问题是我使用了两个循环ant,它打印的是loop1*loop2倍的值,但我只需要$current\u 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
        )
$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 = 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
        )
$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 = 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
        )
$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\u周分配一次总价值,而不是为每个可能的日志分配一次总价值。

这一个应该可以完成这项工作

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

谢谢。。。为了更清晰,我换了几行——我想你也这样做了。