The following functions are important to discus about PHP array search. They are,
The function in_array() is used to check whether a given data element is in a PHP array, return true if found or false if not found. The syntax of this function is,
bool in_array ( mixed needle, array haystack [, bool strict] ).
**If needle is a string, the comparison is done in a case-sensitive manner.
**In PHP versions before 4.2.0 needle was not allowed to be an array.
<?php
$a = array('s','x','y','x', array('a', 'b'), 'o');
echo ((in_array('a', $a))? '"a" is found' : '"a" not found');
echo '<br>'.((in_array('x', $a))? '"x" is found' : '"x" not found');
echo '<br>'.((in_array('Y', $a))? '"Y" is found' : '"Y" not found');
/*
Output:
"a" not found
"x" is found
"Y" not found
*/
?>
In the above example you can notice that element "a" and "Y" are not found in array search. Here "a" is not direct member of the array $a but it is inside an array which is in the array $a. "Y" is not present because the search is case sensitive.
The function array_search() works similer to the function in_array() but return the index of the first occerence of the data found or false if not found. The syntax of this function is,
mixed array_search ( mixed needle, array haystack [, bool strict] ).
** If needle is a string, the comparison is done in a case-sensitive manner.
** Prior to PHP 4.2.0, array_search() returns NULL on failure instead of FALSE.
**Please do not use "==" operator to evaluate the function return value. Because if the needle is found as start element of an array (i.e. index is zero) the function will return zero which will be evaluated to FALSE if use "==" operator. Use operator "===" to evaluate return value.
<?php
$array = array('x', 'y', 'z', 'y');
$key = array_search('x', $array); // $key = 2;
$key = array_search('y', $array); // $key = 1; Index of 1st occerence.
?>
This function is similer to the function array_search() except it returns an array containing the indexes of the array where the needle is found, i.e. if the needle is found in three places the function array_keys() will return an array containing three elements. The syntax of this function is,
array array_keys ( array input [, mixed search_value [, bool strict]] ).
From the above declaration you can see that the search_value is the optional variable. If you do not put any search value this function array_kyes() will return the array of all indexes.
<?php
$a = array('first'=>'s','second'=>'x','third'=>'y','fourth'=>'x', 'fifth'=>array('a', 'b'), 'sixth'=>'o');
echo '<strong>All indexes:</strong>';
foreach(array_keys($a) as $key){echo '<br>'.$key;}
echo '<br><strong>Indexes where data 'x' present</strong>';
foreach(array_keys($a,'x') as $key){echo '<br>'.$key;}
/*
Output:
All indexes:
first
second
third
fourth
fifth
sixth
Indexes where data 'x' present
second
fourth
*/
?>
The function array_key_exists() search array key instead of data hold by an array. When the search key found it returns true and failing returns false. The syntax of array_key_exists is,
bool array_key_exists ( mixed key, array search ).
<?php
$a = array('first'=>'s','second'=>'x','third'=>'y','fourth'=>'x', 'fifth'=>array('a', 'b'), 'sixth'=>'o');
echo '<br>'.((array_key_exists('second',$a))? 'second item found' : 'second item not found');
echo '<br>'.((array_key_exists('ninth',$a))? 'ninth item found' : 'ninth item not found');
/*
Output:
second item found
ninth item not found
*/
?>