1//array_search
2$result = array_search("apple", $fruit_array); // return index or false
3
4//in_array
5$result = in_array("apple", $fruit_array); // return true or false
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<?php
2$a = [
3'Canceled' => 1,
4'Traded / Filled'=> 2,
5'(Not used currently)'=> 3,
6'Transit'=> 4,
7'Rejected'=> 5,
8'Pending'=> 6,
9];
10echo array_search("5",$a);
11?>
12
1// array demo
2
3public $gender = array(
4 1 => 'Male',
5 2 => 'Female',
6 3 => 'Other',
7);
8
9$gen = 1;
10
11if ($gen == array_search('Male', $this->gender)) {
12 $gender = __d('member', 'male');
13
14} elseif ($gen == array_search('Female', $this->gender)) {
15 $gender = __d('member', 'female');
16
17}
18
19
1$userdb = array(
2 array(
3 'uid' => '100',
4 'name' => 'Sandra Shush',
5 'pic_square' => 'urlof100'
6 ),
7 array(
8 'uid' => '5465',
9 'name' => 'Stefanie Mcmohn',
10 'pic_square' => 'urlof100'
11 ),
12 array(
13 'uid' => '40489',
14 'name' => 'Michael',
15 'pic_square' => 'urlof40489'
16 )
17);
18
1PHP function array_search(mixed $needle, array $haystack, bool $strict = false) false|int|string
2---------------------------------------------------------------------------------------------
3
4Searches the array for a given value and returns the first corresponding key if successful.
5
6Parameters:
7mixed--$needle--The searched value.If needle is a string, the comparison is done in a case-sensitive manner.
8array--$haystack--The array.
9bool--$strict--[optional] If the third parameter strict is set to true then the array_search function will also check the types of the needle in the haystack.
10
11Returns: the key for needle if it is found in the array, false otherwise.
12
13If needle is found in haystack more than once, the first matching key is returned. To return the keys for all matching values, use array_keys with the optional search_value parameter instead.