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 { next } from 'locutus/php/array/next'.
Or with CommonJS: const { next } = require('locutus/php/array/next')
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.
#
code
expected result
1
var $transport = ['foot', 'bike', 'car', 'plane']
next($transport)
next($transport)
'car'
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
Uses global: locutus to store the array pointer
Dependencies
This function uses the following Locutus functions:
const findPointerIndex = (pointers: PhpList<PhpInput>, target: PhpInput): number => { for (let index = 0; index < pointers.length; index += 1) { if (pointers[index] === target) { return index } }
return -1 }
function getPointerState<T>(target: PhpArrayLike<T>, initialize = true): PointerState | null { const runtime = ensurePhpRuntimeState() const pointers = runtime.pointers // Store [target, cursor] pairs in one flat runtime list so pointer state stays bound to each array-like input. constpointerTarget: PhpInput = target
let index = findPointerIndex(pointers, pointerTarget) if (index === -1) { if (!initialize) { returnnull } pointers.push(pointerTarget, 0) index = pointers.length - 2 }
return { ini, locales, localeCategories, pointers, locale_default: localeDefault, } }
// php/_helpers/_arrayPointers (Locutus helper dependency) constfindPointerIndex = (pointers, target) => { for (let index = 0; index < pointers.length; index += 1) { if (pointers[index] === target) { return index } }
return -1 }
functiongetPointerState(target, initialize = true) { const runtime = ensurePhpRuntimeState() const pointers = runtime.pointers // Store [target, cursor] pairs in one flat runtime list so pointer state stays bound to each array-like input. const pointerTarget = target
let index = findPointerIndex(pointers, pointerTarget) if (index === -1) { if (!initialize) { returnnull } pointers.push(pointerTarget, 0) index = pointers.length - 2 }
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.