The array_search function searches the specified value in the array and returns its key. If the array is an indexed array, you will get the index of the value found.
What is the syntax of the array_search function in PHP?
array_search(value, array, strict)
Parameters | Details |
---|---|
value | The array to search the value in – Required |
array | The value to search from the array – Required |
strict | Boolean Parameter When set to true: Will does not search the identical elements, that is “5” and 5 will not be considered to be similar. When set to false: Will search the identical elements, that is “5” and 5 will be considered to be similar. – Optional |
Examples of array_search function
<?php
$arr=array("a"=>"Pakistan","b"=>"Iran","c"=>"Turkey");
echo array_search("Turkey",$arr);
?>
In the above example, we use array_search to search “Turkey” from the given array.
<?php
$arr=array("a"=>"1","b"=>1,"c"=>"1");
echo array_search(5,$arr,true);
?>
In the above example, we use array_search to search the specified element from the associative array with a strict mode set to true. Try the above example with strict mode set to false.