in_array()
: Check if a value exists in the arrayarray_search()
: Search the array by valuearray_keys()
: Search the array by valuearray_key_exists()
: Check if a key or index exists in the arrayarray_values()
: Get all the values of an array
in_array()
<?php //Syntax in_array(mixed $needle, array $haystack, bool $strict = false): bool
The in_array()
function returns true
if an array $haystack
contains a specific value $needle
. The following example searches the array of integers $smallPrimes
for the integer 19
:
<?php $smallPrimes = array(3, 5, 7, 11, 13, 17, 19, 23, 29); if (in_array(19, $smallPrimes)) echo "19 is a small prime number"; // Always printed
A third, optional argument can be passed that enforces a strict type check when comparing each element with the needle. In the following example in_array()
by default returns true
; however, with strict type checking, the string "19"
doesn’t match the integer 19
held in the array and returns false
:
<?php $smallPrimes = array(3, 5, 7, 11, 13, 17, 19, 23, 29); if (in_array('19', $smallPrimes, true)) echo "19 is a small prime number"; // NOT printed
array_search()
<?php //Syntax array_search ( mixed $needle, array $haystack, bool $strict = false): int|string|false
The array_search( )
function works the same way as the in_array( )
function, except the key of the matching value $needle
is returned rather than the Boolean value true
. However, if the value isn’t found, array_search( )
returns false
.
The following code shows how array_search( )
works with both associative and indexed arrays:
<?php $measure = array("inch"=>1, "foot"=>12, "yard"=>36); echo array_search(12, $measure); //foot $units = array("inch", "centimeter", "chain", "furlong"); echo array_search("chain", $units); //2
Because array_search( )
returns a mixed result: the Boolean value false
if the value isn’t found or the array key of the matching element. A problem is encountered when the first element is found in an indexed array, PHP’s automatic type conversion treats the value 0, the index of the first element, as false
in a Boolean expression.
Care must be taken with functions, such as array_search()
, that return a result or the Boolean value false
to indicate when a result can’t be determined. If the return value is used as a Boolean (in an expression or as a Boolean parameter to a function) a valid result may be automatically converted to false
. If such a function returns 0
, 0.0
, "c"
, an empty string, or an empty array, PHP’s automatic type conversion converts the result to false
when a Boolean value is required.
The correct way to test the result is to use the is-identical operator ===,
as shown in the following example:
<?php $units = array("inch", "centimeter", "chain", "furlong"); $index = array_search("inch", $units); if ($index === false) echo "Unknown unit: inch"; else // OK to use $index echo "Index = $index";
Note:
This function returns the first corresponding key for a given value, if you want to find all corresponding keys use array_keys()
function instead.
array_keys()
<?php /* Syntax # 1 returns all the keys from the array.*/ array_keys(array $array): array /*Syntax # 2 searches the array for a value and returns a subset of the keys from the array*/ array_keys(array $array, mixed $search_value, bool $strict = false): array
This function returns all the keys of an array. If the optional $search_value
argument is specified, you can get the keys for that particular value.
Example: Get all the keys from the array
<?php $a = ['red','blue','red','green','one'=>1, 'two'=>2]; $keys = array_keys($a); print_r($keys); /* prints: Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => one [5] => two ) */
Example: Search array by value and get corresponding keys
The following code shows how array_keys( )
works with both associative and indexed arrays:
<?php $a = ['red','blue','red','green','one'=>1, 'two'=>2]; $keys = array_keys($a, 'red'); //[0,2] $b = ['one'=>'one', 1=>'one', 2=>'two']; $k = array_keys($b, 'one'); //['one',1]
Note:
Similar to in_array()
and array_search()
functions, a third, optional argument can be passed that enforces a strict type check when comparing each element with the needle.
array_key_exists()
<?php //syntax array_key_exists(string|int $key, array $array): bool
The array_key_exists()
function returns true
if an array $array
contains a specific key $key
. The following example searches the array $numbers
for the key two
:
<?php //Associative array $numbers = ['one'=>1, 'two'=>2, 'three'=>3, 'four'=>'four']; if ( array_key_exists('two', $numbers) ) echo 'The key "two" exists in the array $numbers'; else echo 'Key not found'; /* Prints: The key "two" exists in the array $numbers; */
Key can be any value possible for an array index:
<?php //Indexed array $arr = ['a','b','c',1,2,3]; if ( array_key_exists(2, $arr) ) echo 'The index key 2 value is: '.$arr[2]; else echo 'Key not found'; /* Prints: The index key 2 value is: c */
array_values()
<?php //Syntax array_values(array $array): array
This function returns an indexed array with all the values of the original array. See Example:
<?php //associative array $a = ['one'=>1, 'two'=>2, 'three'=> 3]; //returns an indexed array $b = array_values($a); print_r($b); /*Prints Array ( [0] => 1 [1] => 2 [2] => 3 ) */
Working with arrays: