Array: 4th size, isset return not reliable

Does anyone know how it could be that this code repeats yahoo ? Obviously, there is no fourth array with the key "something", but he thinks about it. Error? Feature?

 $array = array('a' => array('b' => array('c' => 'test'))); echo '<pre>'; var_dump($array); echo '</pre>'; if (isset($array['a']['b']['c']['something'])) { echo 'yahoo'; } 
+4
source share
3 answers

Because PHP thinks you are checking "something" on the line "test". Remember that strings are arrays of characters. try echo $ array ['a'] ['b'] ['c'] ['something'].

:: EDIT ::

I explained this, I did not say that it made sense .: P

+4
source

In this case, you want to use is_array($array['a']['b']['c']) rather than isset($array['a']['b']['c']['something']) , or maybe a crafty combination of the two, to make sure you don't get any errors if it's not installed, when you check if it's an array.

Sort of:

 if(isset($array['a']['b']['c']['something']) && is_array($array['a']['b']['c'])){ [...] } 
+1
source

Here we discussed the behavior of PHP in this problem and presented a solution that is specifically for your problem.

+1
source

All Articles