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$search_array = array('first' => 1, 'second' => 4);
3if (array_key_exists('first', $search_array)) {
4 echo "The 'first' element is in the array";
5}
6?>
7
1
2<?php
3$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
4
5$key = array_search('green', $array); // $key = 2;
6$key = array_search('red', $array); // $key = 1;
7?>
8
9
1$people = array(
2 2 => array(
3 'name' => 'John',
4 'fav_color' => 'green'
5 ),
6 5=> array(
7 'name' => 'Samuel',
8 'fav_color' => 'blue'
9 ));
10$found_key = array_search('blue', array_column($people, 'fav_color'));
1$arr = array(
2 "one" => 1,
3 "two" => 2,
4 "three" => 3,
5 "seventeen" => 17
6);
7function find($mot){
8
9 global $arr; // this is global variable
10 $ok=false;
11 foreach ($arr as $k => $v)
12 {
13 if($k==$mot){
14 return $v; $ok=true; // or return true;
15 }
16 }
17 if(ok==false){ return -1; } // or return false;
18}
19
20//call function
21echo find("two");