How to Check if a Value Is an Array in PHP with is_array
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
$<result> = is_array(<value>);
| Part | What 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:

<?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
php main.php
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:
<?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";
php main.php
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

What Happened
['editor', 'viewer']and[]are arrays, the empty one included.'editor'andnullaren't.- The string
'customer'went to theelsebranch, socount()never saw it. - Without the check,
count('customer')stopped the script withTypeError: count(): Argument #1 ($value) must be of type Countable|array, string given.count()accepts only an array or an object that implements PHP'sCountableinterface. - The error appears twice because this PHP's settings both log errors to the terminal, as
PHP Fatal error:, and display them, asFatal error:.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Calling count() or in_array() on a value that may not be an array | A string or null stops the script with a TypeError | Check is_array first |
Expecting is_array to be true for a JSON object from json_decode | json_decode returns a stdClass object, so is_array is false | Pass true as json_decode's second argument to get an array |
Using is_array to decide whether count() will work | An ArrayObject isn't an array, yet count() accepts it | Use is_countable when countable objects should pass too |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
TypeError: count(): Argument #1 ($value) must be of type Countable|array, string given | count() got a string | Check is_array first |
TypeError: in_array(): Argument #2 ($haystack) must be of type array, null given | in_array() got null | Check 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
- is_array, is_countable and count, in the PHP manual.
