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.
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.
<code>$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</code>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
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.