Install via yarn add locutus and import:
import { array_replace } from 'locutus/php/array/array_replace'.
Or with CommonJS: const { array_replace } = require('locutus/php/array/array_replace')
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.
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.
Dependencies
This function uses the following Locutus functions:
// Although docs state that the arguments are passed in by reference, // it seems they are not altered, but rather the copy that is returned // (just guessing), so we make a copy here, instead of acting on arr itself const arrObject = toPhpArrayObject<TValue>(arr) for (const p in arrObject) { retObj[p] = arrObject[p] }
for (const replacement of [firstReplacement, ...additionalReplacements]) { if (!isObjectLike(replacement)) { continue } const current = toPhpArrayObject<TValue>(replacement) for (const p in current) { retObj[p] = current[p] } }
// Although docs state that the arguments are passed in by reference, // it seems they are not altered, but rather the copy that is returned // (just guessing), so we make a copy here, instead of acting on arr itself const arrObject = toPhpArrayObject(arr) for (const p in arrObject) { retObj[p] = arrObject[p] }
for (const replacement of [firstReplacement, ...additionalReplacements]) { if (!isObjectLike(replacement)) { continue } const current = toPhpArrayObject(replacement) for (const p in current) { retObj[p] = current[p] } }
// Although docs state that the arguments are passed in by reference, // it seems they are not altered, but rather the copy that is returned // (just guessing), so we make a copy here, instead of acting on arr itself const arrObject = toPhpArrayObject<TValue>(arr) for (const p in arrObject) { retObj[p] = arrObject[p] }
for (const replacement of [firstReplacement, ...additionalReplacements]) { if (!isObjectLike(replacement)) { continue } const current = toPhpArrayObject<TValue>(replacement) for (const p in current) { retObj[p] = current[p] } }
return retObj }
// php/_helpers/_phpTypes (Locutus helper dependency) functionisObjectLike(value) { returntypeof value === 'object' && value !== null }
functiontoPhpArrayObject(value) { if (isPhpArrayObject(value)) { return value }
// Although docs state that the arguments are passed in by reference, // it seems they are not altered, but rather the copy that is returned // (just guessing), so we make a copy here, instead of acting on arr itself const arrObject = toPhpArrayObject(arr) for (const p in arrObject) { retObj[p] = arrObject[p] }
for (const replacement of [firstReplacement, ...additionalReplacements]) { if (!isObjectLike(replacement)) { continue } const current = toPhpArrayObject(replacement) for (const p in current) { retObj[p] = current[p] } }
return retObj }
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.