1

This is quite possibly NOT something that can be reproduced through a copy & paste. The issue I am having here is that array_key_exists simply is not working.

I have printed my code to the page; it is definitely of type array, not that that is required. The array key exists; I have cast between both string & integer and it is coming back not found.

Now, where it becomes most puzzling is that I have iterated between

$presets    = $this->presets->$name;
echo gettype($version).'<br>';
foreach ( $presets as $key=>$test ) {
    if ( $key===$version ) echo 'string found1<br>';
    elseif ( $key==$version ) echo 'string found2<br>';
}
if ( !array_key_exists($version,$presets) )
    echo 'array_key_exists string fail.<br>';

$version = intval($version);
echo gettype($version).'<br>';
foreach ( $presets as $key=>$test ) {
    if ( $key===$version ) echo 'int found1<br>';
    elseif ( $key==$version ) echo 'int found2<br>';
}
if ( !array_key_exists($version,$presets) )
    echo 'array_key_exists int fail.<br>';

Output:

string
string found1
array_key_exists string fail.
integer
int found2
array_key_exists int fail.

The output is absolutely strange because in both the int & string searches, the key matches. In fact, in the string search, it outputs "string found1", meaning that it === a key within the array. So, then, why is array_key_exists ALWAYS producing FALSE?

The variable $presets is some array whose indices are created using preg_match regex codes to parse the indices. That is the only added mystery I can come up with, though a manual comparison has proved that an === value was discovered, and therefore none of this should matter. array_key_exists should return TRUE

  Array
  (
      [100] => stdClass Object
          ()
  )
Barmar
  • 741,623
  • 53
  • 500
  • 612
Antago
  • 11
  • 4
  • What is the content of `$presets`? – Felippe Duarte Aug 29 '18 at 21:54
  • 1
    I can't reproduce this. But I also can't create an array whose key is a numeric string. Whenever I try to set the key to a string like `"100"`, it becomes the integer `100`. And your `var_dump()` shows a numeric key as well. – Barmar Aug 29 '18 at 22:14
  • 2
    "_$presets is some array whose indices are created_...". Is it possible that presets is an object that is traversible? What are the outputs of `gettype($presets);`? – jh1711 Aug 29 '18 at 22:16
  • Reproducible when `$presets` is an `stdClass`: `php -r '$p = (object)["100" => new stdClass]; var_dump(array_key_exists("100", $p), array_key_exists(100, $p));'` Output: `bool(false), bool(false)` – weirdan Aug 29 '18 at 22:36
  • @Barmar Maybe like this? https://stackoverflow.com/a/35180513/2734189 – Don't Panic Aug 29 '18 at 22:48
  • @Don'tPanic Interesting. But that doesn't seem to be what's going on here, since the `var_dump()` shows an integer key. But his `foreach()` loops indicate that it's a string key. Something is not right. – Barmar Aug 29 '18 at 22:50
  • the array listed is the dump of $presets. I am going to look into the answer below about "numeric-strings" being a thing in <7.2 ... hmm. Thank you this far! – Antago Aug 30 '18 at 04:11
  • EDIT: I have discovered something that appears to be sweeped under the rug and I don't know the solution necessarily just yet. This is apparently a fatal PHP error albeit apparently only *briefly* encountered. `get_object_vars` was used to convert my `object` to an `array`, and according to a bug report in PHP 7.1, `array_key_exists` won't work with numeric keys from said `get_object_vars` conversion. – Antago Aug 30 '18 at 04:29

1 Answers1

0

Based on the results you got from $key==$version and $key===$version, we know that the key is a numeric string. So it appears you are using a version of PHP < 7.2.

In those versions, you can't reference a numeric string array key, and array_key_exists will return false whether you give the key in numeric or string form. This confusing behavior was improved with PHP 7.2.

Here's a demo illustrating this, based on the example from this answer.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80