0

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.

logos_164
  • 736
  • 1
  • 13
  • 31
  • You can try $newrecord[$i][3] = trim($newrecord[$i][3][0]); – Tom Jul 19 '18 at 00:49
  • Thanks but I'm not sure how that solves it, I'm getting the correct value for $newrecord[$i][3] – logos_164 Jul 19 '18 at 00:50
  • Unrelated to the problem: Your `for` loop is wrong. Array indexes start at 0 and end at `sizeof($methods)-1`. – Barmar Jul 19 '18 at 00:56
  • Try using `var_dump()` rather than `print_r()`, it shows more details that may help diagnose. – Barmar Jul 19 '18 at 01:00
  • thanks @Barmar but I set it at 1 so that it skips the first entry, which is just a title section – logos_164 Jul 19 '18 at 01:12
  • You still need to end at `count($methods)-1`, not `count($methods)` or you'll try to access a nonexistent array element. In other words, change `<=` to `<`. – Barmar Jul 19 '18 at 01:13
  • @Barmar I did as you suggested and used `var_dump` instead of `print_r` and I think i might have found the problem. For the new date, it returns `string(10)` and for the date that is already in the array, it returns `string(19)` do you know if this technically makes them different values? This would explain why the `if()` doesn't work. – logos_164 Jul 19 '18 at 19:41
  • 1
    There's probably some HTML code in there, which you're not seeing in the rendered page. Use View Source to see the actual contents of the strings. – Barmar Jul 19 '18 at 19:43
  • Bingo, there is spacing between the date values. Any idea on how to get rid of that? for instance `2018-05-06` in the array comes out as `2 0 1 8 - 0 5 - 0 6` – logos_164 Jul 19 '18 at 19:47
  • @Barmar check out my answer, you were substantial in helping me diagnose the problem – logos_164 Jul 20 '18 at 14:09

1 Answers1

0

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.

logos_164
  • 736
  • 1
  • 13
  • 31
  • This sounds like an encoding problem. You're getting UTF-16 instead of UTF-8. – Barmar Jul 20 '18 at 14:23
  • Where did `$methods` come from? – Barmar Jul 20 '18 at 14:23
  • $methods comes from another function I run before this function, the problem is in the data from the CSV itself, which is uploaded from a database, so I’m assuming that’s where UTF-16 is coming from – logos_164 Jul 20 '18 at 16:34