4.10. Checking if a Key Is in an Array

Problem

You want to know if an array contains a certain key.

Solution

Use isset( ) :

if (isset($array['key'])) { /* there is a value for 'key' in $array */ }

Discussion

You can check the definedness of an array element just as you’d for any other variable. See the Introduction to Chapter 5 for more information about the truth value of variables.

See Also

Documentation on isset( ) at http://www.php.net/isset.

Get PHP Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.