PHP Arrays
Checking if a Key Is in an Array
Problem
You want to know if an array contains a certain key.Solution
Use array_key_exists() to check for a key no matter what the associated value is:if (array_key_exists('key', $array)) {
/* there is a value for $array['key'] */
}
Use isset() to find a key whose associated value is anything but null:
if (isset($array['key'])) { /* there is a non-null value for 'key' in $array */ }
No comments:
Post a Comment