PHP array key exists() - Check if Key Exists in Array

[Pages:3]PHP array_key_exists() ? Check if Key Exists in Array

PHP array_key_exists() Function

The PHP array_key_exists() function checks if a specific key exists in the array. The function returns TRUE if the key is present, else it returns FALSE. array_key_exists() function works for both indexed arrays and associative arrays. For indexed arrays, index is the key.

Syntax ? array_key_exists()

The syntax of array_key_exists() function is array_key_exists( key, array)

where

Parameter Description

key array

[mandatory] The key which we have to search in an array. [mandatory] The array in which we have to search the key.

Function Return Value array_key_exists() returns boolean value TRUE if the key exists and FALSE if the key does not exist.

Example 1: Check an Array for a Specified Key

In this example, we will take an associative array with key-value pairs, and check if specific key "m" is present in the array.

PHP Program

Output

array_key_exists() returns TRUE for the given array and specified key. So, if block is executed.

Example 2: array_key_exists() for Indexed Array

In this example, we will check if a specific index (key) is present in the array. PHP Program

Output The array is also printed to the console to understand the index of items in the array.

Conclusion

In this PHP Tutorial, we learned how to , using PHP Array array_key_exists() function.

PHP Array Functions

PHP array() PHP array_chunk() PHP array_column() PHP array_combine()

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download