0
0
PhpHow-ToBeginner · 3 min read

How to Check if Element Exists in Array in PHP

In PHP, you can check if an element exists in an array using the in_array() function for values or array_key_exists() for keys. in_array() returns true if the value is found anywhere in the array.
📐

Syntax

The main function to check if a value exists in an array is in_array(). It takes three parameters:

  • needle: The value you want to find.
  • haystack: The array to search in.
  • strict (optional): If true, checks types strictly (default is false).

For checking if a key exists, use array_key_exists() with:

  • key: The key to check.
  • array: The array to check in.
php
<?php
// Check if value exists
bool in_array(mixed $needle, array $haystack, bool $strict = false);

// Check if key exists
bool array_key_exists(mixed $key, array $array);
?>
💻

Example

This example shows how to check if a value exists in an array using in_array() and how to check if a key exists using array_key_exists().

php
<?php
$fruits = ['apple', 'banana', 'orange'];

// Check if 'banana' is in the array
if (in_array('banana', $fruits)) {
    echo "Banana is in the fruits array.\n";
} else {
    echo "Banana is not in the fruits array.\n";
}

// Check if key 1 exists
if (array_key_exists(1, $fruits)) {
    echo "Key 1 exists and its value is: " . $fruits[1] . "\n";
} else {
    echo "Key 1 does not exist.\n";
}

// Check with strict type
$numbers = [1, 2, '3'];
if (in_array(3, $numbers, true)) {
    echo "3 (integer) found with strict check.\n";
} else {
    echo "3 (integer) not found with strict check.\n";
}
?>
Output
Banana is in the fruits array. Key 1 exists and its value is: banana 3 (integer) not found with strict check.
⚠️

Common Pitfalls

One common mistake is confusing in_array() and array_key_exists(). in_array() checks for values, not keys. Also, by default, in_array() does not check types strictly, so '3' (string) and 3 (integer) are considered equal unless you set the third parameter to true.

Another pitfall is using isset() to check keys, but isset() returns false if the value is null, while array_key_exists() returns true.

php
<?php
$array = ['a' => null];

// Wrong: isset returns false if value is null
if (isset($array['a'])) {
    echo "Key 'a' exists (using isset).\n";
} else {
    echo "Key 'a' does not exist (using isset).\n";
}

// Right: array_key_exists returns true even if value is null
if (array_key_exists('a', $array)) {
    echo "Key 'a' exists (using array_key_exists).\n";
} else {
    echo "Key 'a' does not exist (using array_key_exists).\n";
}
?>
Output
Key 'a' does not exist (using isset). Key 'a' exists (using array_key_exists).
📊

Quick Reference

Use this quick guide to remember the functions:

FunctionPurposeParametersReturns
in_array(needle, haystack, strict=false)Check if value exists in arrayneedle: value, haystack: array, strict: booltrue if found, false otherwise
array_key_exists(key, array)Check if key exists in arraykey: key, array: arraytrue if key exists, false otherwise
isset(array[key])Check if key exists and value is not nullarray[key]false if key missing or value is null

Key Takeaways

Use in_array() to check if a value exists anywhere in an array.
Use array_key_exists() to check if a specific key exists, even if its value is null.
Set the third parameter of in_array() to true for strict type checking.
Avoid using isset() to check keys if the value can be null.
Remember in_array() checks values, not keys.