How to check if an array is associative or sequential in PHP?
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.
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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

ArtGPT
AI image generator for creative art from text prompts.

Stock Market GPT
AI powered investment research for smarter decisions

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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.

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.

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

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.

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

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.

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.

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.
