← Back to PHP Course | Chapter 7: Arrays | Lesson 12 of 13

PHP Array Searching

Searching an array is like looking through your backpack to see if your pencil is in there. PHP can tell you whether a value exists and where it sits in the list.
Syntax
php
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
<?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));
?>

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
<?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;
?>

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
<?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));
?>

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
<?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));
?>

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
<?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;
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using in_array on mixed types without the third argument true, so in_array("1e1", ["10"]) is true because of loose comparison.
  2. Testing array_search with if (array_search(...)), which is false when the key is 0; compare with !== false.
  3. Using isset to check a key with a null value, which returns false; use array_key_exists instead.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.