PHP Array Searching
In this page:
in_array($value, $array); // true or false
array_search($value, $array); // key, or false
array_key_exists($key, $array);
Finding Values with in_array
in_array() checks whether a given value exists anywhere in an array and returns a simple boolean, which is the quickest way to answer a yes/no membership question like 'is this email already registered?'
Example: Finding Values with in_array
<?php
// Declare `$emails` as an array: `["[email protected]", "[email protected]"]`
$emails = ["[email protected]", "[email protected]"];
// Print a detailed dump (with types) of `in_array("[email protected]", $emails)`
var_dump(in_array("[email protected]", $emails));
?>
Login to try C/C++/Java/PHP code in the editor
Finding Keys with array_search
array_search() is similar to in_array() but returns the key of the first matching element instead of just true/false, letting you locate exactly where a value lives so you can update or remove it.
Example: Finding Keys with array_search
<?php
// Declare `$fruits` as an array: `["apple", "banana", "cherry"]`
$fruits = ["apple", "banana", "cherry"];
// Declare `$key`, set to `array_search("banana", $fruits)`
$key = array_search("banana", $fruits);
// Print `$key` to the output
echo $key;
?>
Login to try C/C++/Java/PHP code in the editor
Checking Key Existence
array_key_exists() checks for the presence of a key rather than a value, which matters because a key can exist with a null or empty value that in_array() would otherwise miss depending on strict comparison settings.
Example: Checking Key Existence
<?php
// Declare `$data` as an array: `["email" => null]`
$data = ["email" => null];
// Print a detailed dump (with types) of `array_key_exists("email", $data)`
var_dump(array_key_exists("email", $data));
// Print a detailed dump (with types) of `in_array(null, $data, true)`
var_dump(in_array(null, $data, true));
?>
Login to try C/C++/Java/PHP code in the editor
Searching with array_keys
Both in_array() and array_search() accept an optional strict parameter, and passing true forces === comparison instead of the default loose ==, avoiding surprising matches like 0 == abc in older PHP versions.
Example: Searching with array_keys
<?php
// Declare `$values` as an array: `[0, "abc", false]`
$values = [0, "abc", false];
// Print a detailed dump (with types) of `in_array("abc", $values)`
var_dump(in_array("abc", $values));
// Print a detailed dump (with types) of `in_array("abc", $values, true)`
var_dump(in_array("abc", $values, true));
?>
Login to try C/C++/Java/PHP code in the editor
Advanced Search using array_filter
For associative arrays, combining array_column() with array_search() lets you search by a specific field across a list of records, such as finding the array index of the user whose id matches a given value.
Example: Advanced Search using array_filter
<?php
// Declare `$users` as an array: `[["id" => 1, "name" => "Alice"], ["id" => 2, "name" => "Bob"]]`
$users = [["id" => 1, "name" => "Alice"], ["id" => 2, "name" => "Bob"]];
// Declare `$ids`, set to `array_column($users, 'id')`
$ids = array_column($users, 'id');
// Declare `$index`, set to `array_search(2, $ids)`
$index = array_search(2, $ids);
// Print `$index` to the output
echo $index;
?>
Login to try C/C++/Java/PHP code in the editor
- Using
in_arrayon mixed types without the third argumenttrue, soin_array("1e1", ["10"])is true because of loose comparison. - Testing
array_searchwithif (array_search(...)), which is false when the key is 0; compare with!== false. - Using
issetto check a key with a null value, which returns false; usearray_key_existsinstead.
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: