Skip to main content

How to Check if a Value Is an Array in PHP with is_array

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

is_array($value) in PHP returns true when $value is an array, empty ones included, and false for anything else, such as a string, null or an object. Check it before you pass a value to a function that needs an array, such as count().

Syntax​

Syntax
$<result> = is_array(<value>);
PartWhat it does
<value>The value to check. It can be of any type.
$<result>true if <value> is an array, otherwise false.

The PHP manual: is_array "Finds whether the given variable is an array", and it "Returns true if value is an array, false otherwise."

The Example​

The example checks four values: a list of two roles, an empty array, a string and null. Then it counts a user's roles, which arrive as the single string 'customer' instead of a list. It checks the value with is_array before calling count(), then calls count() without the check.

Prerequisites​

  • PHP. This guide uses PHP 8.6.0.

Step 1: Write the Code​

Create a file named main.php:

main.php, which prints is_array for a list of two roles, an empty array, a string and null with var_dump, then sets roles to the string customer and counts it only when is_array says it's an array.

main.php
<?php

declare(strict_types=1);

var_dump(is_array(['editor', 'viewer']));
var_dump(is_array([]));
var_dump(is_array('editor'));
var_dump(is_array(null));

$roles = 'customer'; // a string, where a list of roles was expected
// Check the type first, so count() only ever gets an array.
if (is_array($roles)) {
echo count($roles) . " roles\n";
} else {
echo "roles is not an array\n";
}

var_dump prints a value with its type, so true shows as bool(true).

Step 2: Run It​

Terminal
php main.php
Output
bool(true)
bool(true)
bool(false)
bool(false)
roles is not an array

The command is the same in zsh, bash and PowerShell.

Step 3: Remove the Check​

Replace main.php with a version that counts the roles without checking them:

main.php
<?php

declare(strict_types=1);

$roles = 'customer'; // a string, where a list of roles was expected
// Without the is_array check, count() gets a string.
echo count($roles) . " roles\n";
Terminal
php main.php
Output
PHP Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, string given in /Users/Shared/you/main.php:7
Stack trace:
#0 {main}
thrown in /Users/Shared/you/main.php on line 7

Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, string given in /Users/Shared/you/main.php:7
Stack trace:
#0 {main}
thrown in /Users/Shared/you/main.php on line 7

A zsh terminal where php main.php prints bool(true) twice, bool(false) twice and roles is not an array; for the changed file it stops with PHP Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, string given, shown twice.

What Happened​

  • ['editor', 'viewer'] and [] are arrays, the empty one included.
  • 'editor' and null aren't.
  • The string 'customer' went to the else branch, so count() never saw it.
  • Without the check, count('customer') stopped the script with TypeError: count(): Argument #1 ($value) must be of type Countable|array, string given. count() accepts only an array or an object that implements PHP's Countable interface.
  • The error appears twice because this PHP's settings both log errors to the terminal, as PHP Fatal error:, and display them, as Fatal error:.

Common Mistakes​

MistakeWhat happensDo this instead
Calling count() or in_array() on a value that may not be an arrayA string or null stops the script with a TypeErrorCheck is_array first
Expecting is_array to be true for a JSON object from json_decodejson_decode returns a stdClass object, so is_array is falsePass true as json_decode's second argument to get an array
Using is_array to decide whether count() will workAn ArrayObject isn't an array, yet count() accepts itUse is_countable when countable objects should pass too

Troubleshooting​

SymptomLikely causeFix
TypeError: count(): Argument #1 ($value) must be of type Countable|array, string givencount() got a stringCheck is_array first
TypeError: in_array(): Argument #2 ($haystack) must be of type array, null givenin_array() got nullCheck is_array first

FAQ​

Does json_decode return an array?​

For a JSON array, yes: is_array(json_decode('["editor"]')) was true. For a JSON object it was false, because json_decode returns a stdClass object. With true as the second argument, json_decode('{"role":"editor"}', true) returned an array.

What's the difference between is_array and is_countable?​

is_countable is also true for objects that count() accepts. In my check, new ArrayObject(['editor']) wasn't an array, but is_countable returned true for it and count() returned 1. Both functions returned false for the string 'customer'.

Conclusion​

is_array tells you whether a value really is an array before you hand it to count(), in_array() or anything else that needs one. A check that sends other values elsewhere keeps a string or null from stopping the script.

Additional Resources​