Php array key exists
From w3cyberlearnings
Contents |
PHP function array_key_exists
This function checks array key with a given array. If found, it will return true. If not found, it will return false.
Syntax array_key_exists
- key: key to look for
- array: array
array_key_exists(key,array);
Example 1
<?php $student = array('Abbey' => 89, 'Abbie' => 88, 'Abbigail' => 83, 'Aiyana' => 93); $key = 'Abbie'; if(array_key_exists($key, $student)) { echo "{$key} existed, and her score is {$student[$key]}<br/>"; } ?>
Output
Abbie existed, and her score is 88
Example 2
<?php $fruits = array('orange', 'banana', 'apple'); $key = 0; if (array_key_exists($key, $fruits)) { echo "{$fruits[$key]} is existed.<br/>"; } else { echo 'not existed.<br/>'; } ?>
Output
orange is existed.
Related Links
- array_change_key_case
- array_chunk
- array_combine
- array_count_values
- array_diff_assoc
- array_diff_key
- array_diff_uassoc
- array_diff_ukey
- array_diff
- array_fill_keys
- array_fill
- array_filter
- array_flip
- array_intersect_assoc
- array_intersect_key
- array_intersect_uassoc
- array_intersect_ukey
- array_intersect
- array_key_ exists
- array_keys