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<!DOCTYPE html>
2<html lang="en">
3
4<head>
5 <meta charset="UTF-8">
6 <meta http-equiv="X-UA-Compatible" content="IE=edge">
7 <meta name="viewport" content="width=device-width, initial-scale=1.0">
8 <title>Document</title>
9</head>
10
11<body>
12 <?php
13 if (array_key_exists('btn1', $_POST)) {
14 button1();
15 }
16 if (array_key_exists('btn2', $_POST)) {
17 button2();
18 }
19 function button1()
20 {
21 echo 'btn1 is clicked boom!';
22 }
23 function button2()
24 {
25 echo 'btn2 is clicked wohoo!';
26 }
27 ?>
28 <br><br>
29
30 <form action="" method="post">
31 <button name="btn1">click here</button>
32 <button name="btn2">click here</button>
33 </form>
34</body>
35</html>
1function findKey($array, $keySearch)
2{
3 foreach ($array as $key => $item) {
4 if ($key == $keySearch) {
5 echo 'yes, it exists';
6 return true;
7 } elseif (is_array($item) && findKey($item, $keySearch)) {
8 return true;
9 }
10 }
11 return false;
12}
13
1PHP function key_exists($key, array $array) bool
2------------------------------------------------
3Checks if the given key or index exists in the array. The name of this function is array_key_exists() in PHP > 4.0.6.
4
5Parameters:
6int|string--$key--Value to check.
7array--$array--An array with keys to check.
8
9Returns:true on success or false on failure.