I have a situation where I want to check for a key 2 levels deep in a PHP array. So my code currently looks like...
if(array_key_exists($needleBox, $haystack)) {
if(array_key_exists($needle, $haystack[$needleBox])) {
// do stuff with value attached to $needle
}
}
Where I first check for the array containing my needle ($needleBox), then check for the needle if $needleBox is found.
Is it safe to skip the check for $needleBox & use...
if(array_key_exists($needle, $haystack[$needleBox])) {
// do stuff with value attached to $needle
}
or to check for both together in one if statement like...
if(array_key_exists($needleBox, $haystack) && array_key_exists($needle, $haystack[$needleBox])) {
// do stuff with value attached to $needle
}
I consider the first alternative preferable for being more concise.