dongliang7545 2018-07-19 00:29
浏览 77
已采纳

在php中使用if()来查看数组中的日期是否等于存储在变量中的日期

I have the following code in my index.php:

$d1 = date('Y-m-d', strtotime("-4 days"));
$newrecord = [];
  for($i = 1; $i <= sizeof($methods); $i++) {
      $keys = ["Email", "FirstName", "LastName"];
      $newrecord[$i][0] = $methods[$i][1];
      $newrecord[$i][1] = $methods[$i][2];
      $newrecord[$i][2] = $methods[$i][3];
      $newrecord[$i][3] = explode(" ", $methods[$i][6]);
      $newrecord[$i][3] = $newrecord[$i][3][0];

      $newrecord[$i][4] = $d1;
      if ($newrecord[$i][3] == $newrecord[$i][4]){
        $newrecord[$i][5] = "New Entry";
      }
}

I am expecting to loop through my entire $methods array, and after assigning new values to each of my $newrecord[$i] entries, I need to check whether the value of $newrecord[$i][3] equals the date of 4 days ago.

The problem is that it doesn't look like the if() check is successful, because none of my arrays contain the [5] => "New Entry" array component.

However, I know that there are 120 instances in the array when these do equal each other. For instance:

[23] => Array ( [3] => 2018-07-15 [4] => 2018-07-15 [Email] => me@example.com [FirstName] => John [LastName] => Doe )

Returns for record [23] when I print the output of all the arrays, and there are 120 of them that are like this in total (out of around 8000).

This same thing happens when i just put if($newrecord[$i][3] == $d1) in the if statement.

Any advice on what I'm missing? I appreciate the help.

Note: the values for ["Email"] ["FirstName"] ["LastName"] occur later in the function, this is just a snippet of it to keep it more concise.

  • 写回答

1条回答 默认 最新

  • dongye4192 2018-07-19 23:32
    关注

    The reason the if() statement isn't working is because the true value of the date within the array was not the same as was returned with the $d1 variable.

    This was discovered when I clicked "view page source" on my Chrome browser and inspected the arrays being returned by the var_dump call. Specifically, there were spaces between the characters (i.e. 2018-07-15 became 2 0 1 8 - 0 5 - 0 6).

    Further investigation revealed little black diamonds with "?" inside them in the CSV file so I'm assuming the character code was wrong when importing these.

    The solution, after much trial and error, was to use preg_replace() in the following format:

    $newrecord[$i][3] = preg_replace('/[\x00-\x1F\x7F-\xFF]/', '', $newrecord[$i][3]);
    

    Where /[\x00-\x1F\x7F-\xFF]/ is the search for unwanted characters, I took from this question

    When ran, the if() statement executed just fine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示
  • ¥15 php 同步电商平台多个店铺增量订单和订单状态
  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题