is_array

Checks whether a variable is an array.

Signature
is_array(mixed $value): bool
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$valuemixedThe variable being evaluated.

Return value

booltrue if $value is an array, false otherwise.

is_array() answers one narrow question: is this value a real PHP array? It is a pure type check with no coercion, so it is true for [], for a packed list and for a string-keyed map alike, and false for everything else — including objects that behave like arrays.

That last point is where people get caught. ArrayObject, ArrayIterator, a Doctrine collection and any class implementing ArrayAccess all support $x['key'], yet is_array() returns false for every one of them. Pick the check that matches what you are about to do:

  • About to foreach? Use is_iterable() — it also accepts Traversable.
  • About to count()? Use is_countable().
  • About to use $x['k']? Use is_array($x) || $x instanceof ArrayAccess.
  • Genuinely need array functions like array_map()? Then is_array() is right.

When to reach for it

Typical uses are guarding a recursive walk over decoded JSON or YAML, branching in a config merger where a node may be a scalar or a nested array, and validating a value before handing it to one of the array_* functions, which throw a TypeError on anything else.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Guarding a recursive walk over json_decode() output where a node may be a scalar or a nested array
  • Deciding whether a config value is a single string or a list before merging defaults
  • Validating a mixed parameter before passing it to array_map() or array_merge(), which throw a TypeError

Frequently asked questions

Does is_array() return true for ArrayObject or ArrayAccess objects?

No. Those are objects, so is_array() returns false even though they support square-bracket access. Use is_countable(), is_iterable() or an instanceof ArrayAccess check instead.

How do I check whether an array is a list rather than a map?

Use array_is_list(), added in PHP 8.1. It returns true only when the keys are sequential integers starting at 0, which is what json_encode() needs to produce a JSON array.

Is is_array() faster than gettype($x) === 'array'?

Yes. is_array() is a direct type check compiled to a fast opcode, while gettype() builds and compares a string. Always prefer the is_*() functions in hot paths.

More examples

Recursively flattening decoded JSON

is_array() is what decides between recursing and recording a leaf. The output shows keys a, b.c, b.d.0 and b.d.1 — lists and maps are treated identically because is_array() does not distinguish them.

Example · php
<?php
$data = json_decode('{"a":1,"b":{"c":2,"d":[3,4]}}', true);

function flatten(array $in, string $prefix = ''): array {
    $out = [];
    foreach ($in as $k => $v) {
        $key = $prefix === '' ? (string) $k : "$prefix.$k";
        $out += is_array($v) ? flatten($v, $key) : [$key => $v];
    }
    return $out;
}

print_r(flatten($data));

Array-like objects are not arrays

The object supports $obj['a'] and count(), but is_array() still says false. If your guard is is_array() only, code paths using ArrayObject will be silently skipped — check the capability you need instead.

Example · php
<?php
$obj = new ArrayObject(['a' => 1]);

var_dump($obj['a']);
var_dump(is_array($obj));
var_dump(is_countable($obj));
var_dump(is_iterable($obj));
var_dump($obj instanceof ArrayAccess);

Lists versus maps

is_array() is true for both, but only the list encodes to a JSON array; the gapped-key map becomes a JSON object. When JSON shape matters, pair is_array() with array_is_list().

Example · php
<?php
$list = ['x', 'y'];
$map  = [1 => 'x', 3 => 'y'];

var_dump(is_array($list), is_array($map));
var_dump(array_is_list($list), array_is_list($map));

echo json_encode($list), "\n";
echo json_encode($map), "\n";

Discussion

  • Be the first to comment on this lesson.