Table of Contents
Check for Sequential Array
Check for Associative Array
Examples
Home Backend Development PHP Tutorial How to check if an array is associative or sequential in PHP?

How to check if an array is associative or sequential in PHP?

Sep 16, 2025 am 05:40 AM
php array

An array is sequential if its keys match a continuous integer sequence starting from 0, determined by comparing array_keys($array) with array_keys(array_keys($array)); otherwise, it is associative.

How to check if an array is associative or sequential in PHP?

To check if an array is associative or sequential in PHP, you need to examine the array's keys. A sequential array has integer keys in order starting from 0, while an associative array contains non-integer keys or integer keys that are not in sequence starting from 0.

Check for Sequential Array

A reliable way to determine if an array is sequential is to compare its keys with a generated range of integers from 0 to the count of elements minus one.

  • Use array_keys() to get all the keys of the array.
  • Check if all keys are integers and match the expected sequence.

Here’s a function to check if an array is sequential:

function isSequential($array) {
    $keys = array_keys($array);
    return $keys === array_keys($keys);
}

This works because array_keys($keys) generates a sequential integer array (0, 1, 2, ...), and if the original keys match this, the array is sequential.

Check for Associative Array

An associative array fails the sequential test. So you can define an associative array as one that is not sequential.

  • If the keys are not a continuous sequence starting from 0, it's considered associative.
  • Arrays with string keys or gaps in integer keys will return false in the sequential check.

Function to check if an array is associative:

function isAssociative($array) {
    $keys = array_keys($array);
    return $keys !== array_keys($keys);
}

This returns true if the array has non-sequential or non-numeric keys.

Examples

  • Sequential: [ 'apple', 'banana', 'cherry' ] → keys are 0, 1, 2
  • Sequential (explicit): [ 0 => 'a', 1 => 'b', 2 => 'c' ]
  • Associative: [ 'name' => 'John', 'age' => 30 ]
  • Associative (non-sequential integers): [ 1 => 'a', 3 => 'b' ]

Note: PHP does not have a built-in function for this, so using array_keys() comparison is the most accurate method.

Basically, just compare the array keys against a generated sequence — if they don't match, it's associative. That’s the most reliable approach.

The above is the detailed content of How to check if an array is associative or sequential in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

ArtGPT

ArtGPT

AI image generator for creative art from text prompts.

Stock Market GPT

Stock Market GPT

AI powered investment research for smarter decisions

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Aisi Assistant's genuine download portal_Aisi Assistant's iPhone installation link Aisi Assistant's genuine download portal_Aisi Assistant's iPhone installation link Sep 16, 2025 am 11:30 AM

The official download portal of Aisi Assistant is located on the official website https://www.i4.cn/, and provides computer and mobile downloads, supporting device management, application installation, mode switching, screen projection and file management functions.

How to convert an object to an array in PHP? How to convert an object to an array in PHP? Sep 14, 2025 am 03:14 AM

Use (array) to convert simple objects into arrays. If they contain private or protected properties, the key names will have special characters; for nested objects, recursive functions should be used to traverse the conversion to ensure that all hierarchical objects become associative arrays.

How to prevent XSS (Cross-Site Scripting) attacks in PHP? How to prevent XSS (Cross-Site Scripting) attacks in PHP? Sep 15, 2025 am 12:10 AM

PreventXSSbyescapingoutputwithhtmlspecialchars()orjson_encode(),validatinginputusingfilter_var(),applyingCSPheaders,andusingsecureframeworkslikeLaravel.

How to get POST data in PHP? How to get POST data in PHP? Sep 16, 2025 am 01:47 AM

Use the $_POST hyperglobal array to obtain POST data, read the value through the form name attribute, and use a foreach loop when processing array input, so that the data needs to be verified and filtered to prevent XSS.

How to make an API call using cURL in PHP? How to make an API call using cURL in PHP? Sep 15, 2025 am 05:16 AM

InitializecURLwithcurl_init(),setoptionslikeURL,method,andheaders,senddatausingPOSTorcustommethods,handleresponseviacurl_exec(),checkerrorswithcurl_error(),retrievestatususingcurl_getinfo(),decodeJSONresponse,andclosewithcurl_close().

How to get the request method (GET, POST, PUT) in PHP? How to get the request method (GET, POST, PUT) in PHP? Sep 16, 2025 am 04:17 AM

Use $_SERVER['REQUEST_METHOD'] to obtain HTTP request methods, such as GET, POST, PUT, DELETE; for PUT and other methods, you need to read the original data through file_get_contents('php://input'), and use the switch statement to process different request types.

How to add a watermark to an image in php How to add a watermark to an image in php Sep 15, 2025 am 03:26 AM

Use PHP's GD library to add watermarks to images. First load the original image and watermark (text or image), then use imagecopy() or imagettftext() to merge, and finally save the output. Support JPEG, PNG and other formats, pay attention to handling transparency and font paths, and ensure that GD extension is enabled.

Qushu.com downbook update portal_Dangshu.com bookdown e-book download Qushu.com downbook update portal_Dangshu.com bookdown e-book download Sep 16, 2025 am 10:18 AM

The latest access address of Qushu.com downbook is https://downbook.cc/. The platform provides rich e-book resources, covering a variety of topics, supports customized reading settings, night mode, offline downloads and synchronized reading progress across devices, ensuring that users have a smooth and comfortable reading experience.

See all articles