Please wait

Validating Arrays

PHP offers a few functions for validating and searching through arrays. Let's go through each of them.

Checking for a value

Sometimes, you may need to verify a value exists within an array. The in_array() function in PHP is used to check if a certain value exists in an array. It'll return a boolean. TRUE if the value could be found. Otherwise, FALSE is returned.

This is how it's typically structured:

 in_array(mixed $needle, array $haystack, bool $strict = false): bool


  1. $needle: This is the value that you are looking for in the array.
  2. $haystack: This is the array in which you want to search.
  3. $strict (optional): This determines if you want to do a strict search (also matching the data type) or not. If set to true, the function checks the types of $needle and the array elements.

Let's see it in action:

Basic Example

In this example, we have an array $fruits that contains several types of fruits. We are using in_array() to check if "banana" is in the array. If it is, the message "Banana is found!" is printed. If it isn't, "Banana is not found!" is printed.

The script will output: Banana is found! because "banana" is indeed in the array.

$fruits = ["apple", "banana", "mango", "orange"];
if(in_array("banana", $fruits)) {
  echo "Banana is found!";
} else {
  echo "Banana is not found!";

Strict Comparison

To perform a strict comparison using in_array(), you would set the third parameter of the function to true. A strict comparison not only checks the values but also checks the types of the values.

In this case, the message "Integer 1 is found!" will be printed, even though the string "1" is also in the array. That's because we're using strict comparison, which checks for both the value and the type. The integer 1 and the string "1" are not considered identical in a strict comparison.

$array = array(1, "1", "apple");
if(in_array(1, $array, true)) {
    echo "Integer 1 is found!";
} else {
    echo "Integer 1 is not found!";

Checking if a key exists

The in_array() function is useful for checking if a value exists, but what if you want to check if a key exists with an associative array? In this case, you can use the array_key_exists() function.

The array_key_exists() function in PHP checks if a specific key is present in an array. This function is particularly useful when dealing with associative arrays, where you're often interested in whether a certain key exists.

The function structure is as follows:

array_key_exists(string|int $key, array $array): bool
  1. $key: This is the key you want to check for.
  2. $array: This is the array in which you're searching for the key.

The function returns true if the key is found in the array and false otherwise.

Basic Example

In this example, the array $car has keys ("brand", "model", "year") and corresponding values ("Tesla", "Model 3", 2022). We are checking if the key "model" exists in this array using array_key_exists().

The output will be "Key exists!" because "model" is indeed a key in the $car array. If we searched for a key that is not in the array, for example "color", the output would be "Key does not exist!".

$car = [
  "brand" => "Tesla",
  "model" => "Model 3",
  "year" => 2022
if(array_key_exists("model", $car)) {
  echo "Key exists!";
} else {
  echo "Key does not exist!";

array_key_exists() vs isset()

Oftentimes, you may see some developers use the isset() function. Unlike the array_key_exists() function, this function can check if a value exists for any type of value, not just arrays.

The isset() function in PHP is used to determine if a variable is set and is not null. For example:

$var = "Hello, world!";
if (isset($var)) {
  echo "Variable is set!";
} else {
  echo "Variable is not set!";

In this example, since $var is set and not null, "Variable is set!" will be printed.

Now, when working with arrays, both isset() and array_key_exists() can check if a key exists in an array. However, there's an important difference: isset() also checks if the value corresponding to that key is not null, whereas array_key_exists() only checks if the key exists, regardless of its value.

Here's an example to illustrate the difference:

$array = array("key1" => "value1", "key2" => null);
var_dump(isset($array["key2"])); // This will output: bool(false)
var_dump(array_key_exists("key2", $array)); // This will output: bool(true)

As you can see, isset() returns false because even though "key2" exists in the array, its corresponding value is null. On the other hand, array_key_exists() returns true because it only checks if the key exists, regardless of its value.

So why would you use array_key_exists() over isset()? If you're working with an array and you want to check if a key exists, regardless of whether its value is null or not, you would use array_key_exists(). If you want to ensure the key exists and its value is not null, you would use isset().

Retrieving a value from an array

As you know, you can access an item from an array with a given index, like $arr[2]. But what if you don't know the exact index? In this case, you can search through the array with the array_search() function.

The array_search() function in PHP is used to search an array for a specific value and return the key for the first matching element found. If the value is found in the array more than once, the first matching key is returned. If the value is not found, false is returned.

Here's the basic usage of array_search():

array_search(mixed $needle, array $haystack, bool $strict = false): int|string|false


  1. $value: The value to search for.
  2. $array: The array to search within.
  3. $strict (optional): Whether to search for identical elements (i.e., also checks the type of the value), false by default.

For example, consider the following array of fruits:

$fruits = array("apple", "banana", "cherry", "date");
$index = array_search("cherry", $fruits);
echo $index; // Output: 2

In this case, "cherry" is found at index 2 in the array (remember, arrays in PHP start at index 0), so the function array_search() returns 2.

Keep in mind that array_search() returns the key of the first matching value, not all matching values. If you need to find all occurrences of a value, you would need a different approach, such as using a loop to go through the array.

Counting Items

You can count items in an array with the count() function. The count() function in PHP is used to return the number of elements in an array. It's similar to how length functions work in some other programming languages.

 count(Countable|array $value, int $mode = COUNT_NORMAL): int


  1. $value: The array you want to count the number of elements in.
  2. $mode (optional): It's an optional parameter that can be set to 1 or COUNT_RECURSIVE to count items in nested arrays. The default is 0 or COUNT_NORMAL which does not count nested items.

Take the following example:

$fruits = array("Apple", "Banana", "Cherry");
echo count($fruits); // Output: 3

In this case, we have an array $fruits with three elements. When we call count($fruits), it returns 3 because there are three items in the array.

Key Takeaways

  • in_array(): Checks if a value exists in an array.
  • array_key_exists(): Checks if a key exists in an array.
  • isset(): Checks if a variable is set and is not null.
  • array_search(): Searches an array for a value and returns the first key with that value.
  • count() Counts the number of items in an array.


Please read this before commenting