PHP 8.1 Introduces the array_is_list() Function

PHP 8.1 introduces the built‑in array_is_list() function, which determines whether an array’s keys form a sequential list starting at zero, simplifying checks that previously required manual verification, and the article provides usage examples, code snippets, and a link to the RFC.

php Courses
php Courses
php Courses
PHP 8.1 Introduces the array_is_list() Function

This article is a translation of a blog post originally published at stitcher.io , describing a new feature added in PHP 8.1.

Developers often need to verify that an array’s keys are ordered numerically from 0 without gaps, a check that influences how json_encode decides to encode the array as a JSON list or object.

PHP 8.1 adds a native function array_is_list() that returns true when the array meets these list semantics and false otherwise.

$list = ["a", "b", "c"];<br/>array_is_list($list); // true<br/><br/>$notAList = [1 => "a", 2 => "b", 3 => "c"];<br/>array_is_list($notAList); // false<br/><br/>$alsoNotAList = ["a" => "a", "b" => "b", "c" => "c"];<br/>array_is_list($alsoNotAList); // false

For full details see the RFC: https://wiki.php.net/rfc/is_list .

Related PHP 8.1 feature articles include:

Enums

Readonly properties

Initializers

Fibers with a grain of salt

String-key unpacking

Pure intersection types

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendPHParray_is_listphp81
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.