PHP array_search()

array_search() searches an associative array for a specified value and returns its key if found, otherwise false.    mixed array_search(mixed needle, array array [, bool strict = false])

<?PHP
	$arr=array("apple"=>"carbon","rice"=>"carbon","nuts"=>"fat","meat"=>"protein");
    $key=array_search("fat",$arr);
	echo $key; //nuts 
?>

If there are more than one key with the same value, the first occurrence will be returned. If the strict parameter is true, then the value comparison will be type sensitive.

<?PHP
	$arr=array("apple"=>"carbon","rice"=>"carbon","nuts"=>"fat","meat"=>"protein");
    $key=array_search("carbon",$arr);
	echo $key; //apple
	
    $arr=array("apple"=>"carbon","rice"=>"carbon","nuts"=>"43","meat"=>"protein");
    $key=array_search(43,$arr, true);
    echo $key; //no output, 43 and "43" types not match
	
    $key=array_search("43",$arr, true);
    echo $key; //nuts
?>

If all keys with the same value need to be retrieved, you can use array_keys() function.

<?PHP
	$arr=array("apple"=>"carbon","rice"=>"carbon","nuts"=>"fat","meat"=>"protein");
    $arr2=array_keys($arr,"carbon");
	foreach($arr2 as $element) echo "$element, "; //apple, rice, 
    $arr2=array_keys($arr,"fat");
	foreach($arr2 as $element) echo "$element, "; //nuts,  
?>

in_array() checks whether a value is in a array.

<?PHP
	$arr=array(1,2,3,4,5,6,7,8,9);
    if (in_array(3,$arr)) echo "3 is an array element";
	//3 is an array element
	
    $arr=array("2.3",4,9,10);
	if (in_array("4",$arr,true)) echo "4 is in the array";
	if (in_array(2.3,$arr,true)) echo "2.3 is in the array";
	//no output, type not match. "2.3" is string, 2.3 is number
?>



:: PHP Tutorials Home ::
PHP String Functions
 • concatenation • echo
 • ereg • ereg_replace
 • explode • htmlspecialchars
 • preg_match • preg_replace
 • preg_split • print,sprintf
 • regular expr. • str_replace
 • strcmp • strpos
 • strrev • strrpos
 • strtr • substr
 • substr_replace
PHP Array Functions
 • array_diff • array_flip
 • array_intersect • array_key_exists
 • array_keys • array_merge
 • array_pop • array_push
 • array_rand • array_search
 • array_splice • array_unshift
 • array_values • asort & arsort
 • count • in_array
 • ksort, krsort • shuffle
 • sort
PHP Data Types
 • array • associative array
 • date & time • number
 • class, object • regular expression
 • string • variables
PHP Loop & Conditions
 • continue & break • for loop
 • foreach • if else
 • not equal • while
PHP File System Functions
 • copy • delete, unlink
 • dirname • download url
 • file_exists • is_file, filesize
 • mkdir, rmdir • read file
 • scandir • write file
PHP Popular Topics
 • ajax • clone
 • comments • constants
 • cookie • database
 • defined • die
 • form validation • gd, draw images
 • global variables • header url
 • heredoc • mail
 • pass by reference • print
 • regular expr. • sessions
 • threads • xml parse
PHP Math Functions
 • abs • cos
 • exp • floor & ceil
 • fmod • log
 • max & min • pow
 • round • sin
 • sqrt • tan
endmemo.com © 2024  | Terms of Use | Privacy | Home