1// Here's our fruity array
2$fruits = ['apple', 'pear', 'banana'];
3
4// Use it in an `if` statement
5if (array_key_exists("banana", $fruits)) {
6 // Do stuff because `banana` exists
7}
8
9// Store it for later use
10$exists = array_key_exists("peach", $fruits);
1
2<?php
3$search_array = array('first' => null, 'second' => 4);
4
5// returns false
6isset($search_array['first']);
7
8// returns true
9array_key_exists('first', $search_array);
10?>
11
12
1<?php
2
3// The values in this arrays contains the names of the indexes (keys)
4// that should exist in the data array
5$required = array('key1', 'key2', 'key3');
6
7$data = array(
8 'key1' => 10,
9 'key2' => 20,
10 'key3' => 30,
11 'key4' => 40,
12);
13
14if (count(array_intersect_key(array_flip($required), $data)) === count($required)) {
15 // All required keys exist!
16}
17