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 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?