0

A sample of my associative array is as follows:

// Sets Array
["modern masters 2015"]=>
string(3) "MM2"
["modern masters"]=>
string(3) "MMA"
["mercadian masques"]=>
string(3) "MMQ"

The element that is giving me trouble is "modern masters 2015." Here is a snippet of the code:

$currentSet = "Modern Masters 2015";
$currentSet = strtolower($currentSet);
var_dump($sets[$currentSet]);

When I do var_dump(), it returns NULL only for this particular set. The sets array has over 100 elements, all of which work in the above code EXCEPT "modern masters 2015." However, if I call:

var_dump($sets["modern masters 2015"]);

...it works just fine and returns "MM2" as desired. It's only when I use a variable in between the brackets it returns null (for some odd reason).

What is also weird is that this particular set did not used to be bugged. I speculated that it may have been the integers, but other sets with integers in the name worked perfectly. I'm not too sure what is going on here; some guidance would be appreciated.

romelako
  • 3
  • 4
  • 2
    Check the true contents of the variable `$currentSet`. Verify that its `strlen()` is actually 19, and that there is not an invisible control character in there causing it not to match. Or more simply, trailing whitespace -- `trim($currentSet)` – Michael Berkowski May 30 '15 at 16:59
  • The code as posted is correct. – Michael Berkowski May 30 '15 at 17:01
  • Performing a `trim()` on `$currentSet` did not fix the problem. I also trimmed "modern masters 2015" prior to indexing it into the array and that did not work as well. It just baffles me that it would work then randomly stopped working. – romelako May 30 '15 at 17:22
  • This is weird. Doing a `var_dump($currentSet)` returns a string length of **22**. Mind you, `$currentSet` is being read from a .txt file, and doesn't have any special encoding. Trimming `$currentSet` prior to calling `var_dump($currentSet)` still returns the same length of **22**. Very weird. Any ideas? – romelako May 30 '15 at 17:32
  • See http://stackoverflow.com/questions/1057572/how-can-i-get-a-hex-dump-of-a-string-in-php to inspect the string's individual character hex values to figure out what the offending characters are. – Michael Berkowski May 30 '15 at 17:48

1 Answers1

0

Problem had nothing to do with my code. Was a problem on with the file that the program was reading from. "Modern Masters 2015" was the first line of the file and was being read into the program with a length of 22, despite having an actual length of 19. There must have been some invisible characters in front or at the end, causing the length to increase to that number, and was not removed if a trim() operation was performed on it.

romelako
  • 3
  • 4