PHP's array_is_list in TypeScript

✓ Verified: PHP 8.3
Examples tested against actual runtime. CI re-verifies continuously. Only documented examples are tested.

How to use

Install via yarn add locutus and import: import { array_is_list } from 'locutus/php/array/array_is_list'.

Or with CommonJS: const { array_is_list } = require('locutus/php/array/array_is_list')

Use a bundler that supports tree-shaking so you only ship the functions you actually use. Vite, webpack, Rollup, and Parcel all handle this. For server-side use this is less of a concern.

Examples

These examples are extracted from test cases that automatically verify our functions against their native counterparts.

#codeexpected result
1array_is_list(['a', 'b', 'c'])true
2array_is_list({'0': 'a', '2': 'b'})false
3array_is_list({})true

PHP arrays and TypeScript/JavaScript

Please note that Locutus uses TypeScript/JavaScript objects as substitutes for PHP arrays, they are the closest we can get to this hashtable-like data structure without rolling our own. While many TypeScript/JavaScript implementations preserve the order of object properties, the ECMAScript Language Specification explicitly states that:

The mechanics and order of enumerating the properties is not specified.

In practice most engines preserve insertion order, but if your code depends on key ordering across platforms, keep this caveat in mind.

To influence how Locutus treats objects as arrays, you can check out the locutus.objectsAsArrays setting.

Notes

  • Plain JS objects cannot preserve PHP insertion order for integer-like associative keys.

Dependencies

This function uses the following Locutus functions:

Here's what our current TypeScript equivalent to PHP's array_is_list looks like.

import { normalizeArrayKey, type PhpArrayLike } from '../_helpers/_phpTypes.ts'

export function array_is_list<T>(input: PhpArrayLike<T>): boolean {
// discuss at: https://locutus.io/php/array_is_list/
// parity verified: PHP 8.3
// original by: Kevin van Zonneveld (https://kvz.io)
// note 1: Plain JS objects cannot preserve PHP insertion order for integer-like associative keys.
// Use real JS arrays when numeric key order matters for PHP-style list detection.
// example 1: array_is_list(['a', 'b', 'c'])
// returns 1: true
// example 2: array_is_list({'0': 'a', '2': 'b'})
// returns 2: false
// example 3: array_is_list({})
// returns 3: true

// Integer-like keys on plain JS objects are always enumerated numerically by the language runtime,
// so PHP insertion-order semantics for associative numeric keys can only be preserved with real arrays.
const keys = Object.keys(input)
for (let i = 0; i < keys.length; i++) {
if (normalizeArrayKey(keys[i] ?? '') !== i) {
return false
}
}

return true
}

Improve this function

Locutus is a community effort following The McDonald's Theory: we ship first iterations, hoping others will improve them. If you see something that could be better, we'd love your contribution.

View on GitHub · Edit on GitHub · View Raw


« More PHP array functions


Star