Content · Programming · PHP

PHP is_array() Function: Usage and Examples

Understand is_array() in PHP with syntax, working code examples, expected output, practical use cases, and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

When writing PHP applications, you may need to detect whether a PHP value is an array before using array operations. This is useful in everyday tasks such as working with imported data, application settings, user-facing lists, and reports.

The is_array() operation provides this behavior directly. The examples below show what happens to the inputs and what to check before using the result in your application.

What Is is_array() in PHP?

Detect whether a PHP value is an array before using array operations.

Its syntax is:

is_array(mixed $value): bool

Parameters used by this operation:

  • $value — The value supplied for this argument affects the operation.

Objects implementing ArrayAccess are not arrays. is_array() only checks the type, not whether specific keys exist or whether values are valid.

Example 1: Basic Usage

Start with a small, self-contained input so you can inspect exactly how the operation behaves.

<?php

$settings = ["theme" => "dark"];
var_dump(is_array($settings));

Output:

bool(true)

The output shows the result of the first call. Check the printed keys and values as well as their order.

Example 2: Another Important Case

This variation highlights another input shape, a comparison option, or a change in the collection.

<?php

$payload = json_decode('{"status":"ok"}', true);
var_dump(is_array($payload));

Output:

bool(true)

Compare this result with Example 1: the difference comes from the input or option selected, not from additional processing.

Practical Example: Reject unexpected decoded JSON values before processing a collection

Reject unexpected decoded JSON values before processing a collection. Save the code in a PHP file and adapt the sample data to your project.

<?php

$decodedData = json_decode('{"roles":["viewer","editor"]}', true);
if (is_array($decodedData) && isset($decodedData["roles"]) && is_array($decodedData["roles"])) { echo "Roles: " . count($decodedData["roles"]); }

Output:

Roles: 2

The output comes from the completed workflow shown above. When adapting it, change the source records and keep the same checks for missing, unexpected, or invalid values.

Important Behavior and Edge Cases

Objects implementing ArrayAccess are not arrays. is_array() only checks the type, not whether specific keys exist or whether values are valid.

  • Test the operation with empty or missing input and decide how your application should respond.
  • Read the return value independently from any changes to variables passed by reference.
  • Validate untrusted input before using the result for permissions, database changes, or user-visible output.

Comparison With Related Functions

Function or ApproachWhen to Use It
is_array()Detect whether a PHP value is an array before using array operations
Related functionsis_iterable() also accepts Traversable objects; array_key_exists() checks keys inside arrays.

Common Beginner Mistakes

MistakeCorrect Approach
Assuming input and result have identical keysCheck whether the function reindexes keys or retains original associations.
Confusing mutation with a returned copyObjects implementing ArrayAccess are not arrays. is_array() only checks the type, not whether specific keys exist or whether values are valid.
Choosing a related function with different behavioris_iterable() also accepts Traversable objects; array_key_exists() checks keys inside arrays.

Key Takeaways

  • is_array() helps you detect whether a PHP value is an array before using array operations.
  • Objects implementing ArrayAccess are not arrays. is_array() only checks the type, not whether specific keys exist or whether values are valid.
  • is_iterable() also accepts Traversable objects; array_key_exists() checks keys inside arrays.
  • Use the practical example as a starting point, but validate real application inputs.

is_array() is useful when you need to detect whether a PHP value is an array before using array operations. Use its documented rules and the examples above to choose the right behavior for your data.

Share this resourceSend this page to someone who may find it useful.