PHP array_search() Function
In this chapter you will learn:
- Definition for PHP array_search() Function
- Syntax for PHP array_search() Function
- Parameter for PHP array_search() Function
- Example for PHP array_search() Function
- Example - Search an array for the value 5 and return its key
Definition
The array_search() function search an array for a value and returns the key.
Syntax
PHP array_search() Function has the following syntax.
array_search(value,array,strict)
Parameter
Parameter | Is Required | Description |
---|---|---|
value | Required. | Value to search for |
array | Required. | Array to search in |
strict | Optional. | If go strict |
Possible values:
- true - search for identical elements in the array. the number 3 is not the same as "3"
- false - Default.search for value in the array. the number 3 is the same as "3"
Example 1
Search an array for an element
<?php
$a=array("a"=>"Java","b"=>"PHP","c"=>"java2s.com");
echo array_search("PHP",$a);
?>
The code above generates the following result.
Example 2
Search an array for the value 5 and return its key. Pay more attention to the "".
<?php
$a=array("a"=>"5","b"=>5,"c"=>"5");
echo array_search(5,$a,true);
?>
The code above generates the following result.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP array_shift() Function
- Syntax for PHP array_shift() Function
- Parameter for PHP array_shift() Function
- Example - Returns the value from the front of the array and remove it from the array
- Example - Remove elements from an associate array
Home » PHP Tutorial » PHP Array Functions