PHP Array Search
PHP Array Search will Searches the array for a given value and returns the corresponding key if successful. Array Search PHP function will accepts three arguments, first argument is the value to search in array, second argument is the array to search, third argument is STRICT If the third parameter strict is set to TRUE then the array_search() function will search for identical elements in the array. This means it will also check the types of the value in the array, and objects must be the same instance.
Syntax of PHP Array Search function
Array Search accepts three arguments, first one is value to be search in array. Second argument is the array. Third argument is strict, If this parameter is set to TRUE, then this function will search for identical elements in the array. Possible values:
- false – Default
array_search ("value" , $array , $strict)
Example code for using Array Search PHP function
This is simple example code,
$array is an simple array with keys and values, in the next statements using array_search function searching for a value in the result it will gives the key. Just use echo then you can see these results.
<?php $array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red'); $key = array_search('green', $array); // $key = 2; $key1 = array_search('red', $array); // $key1 = 1; ?>
Searching for a value in array using strict to true
If the third argument strict is set to TRUE then the array_search() function will search for identical elements in the array. This means it will also check the types of the value in the array, and objects must be the same instance.
Here is an example code snippet with strict set to TRUE. in this array a element is set to 10 but it’s an string, but b element is an integer. In the array_search value filed is an integer, so the output should be an integer “key” => “value” pair.
<?php $a=array("a"=>"10","b"=>10); echo array_search(10, $a, true); //output is b ?>
If you have any problem using array search function in php, then let me know using comment form below.
Incoming search terms:
- array search in php
- php array search
- syndax of array search in php