PHP's array_replace in TypeScript

How to use

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.

#codeexpected result
1array_replace(["orange", "banana", "apple", "raspberry"], {0 : "pineapple", 4 : "cherry"}, {0:"grape"}){0: 'grape', 1: 'banana', 2: 'apple', 3: 'raspberry', 4: 'cherry'}

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.

Dependencies

This function uses the following Locutus functions:

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

import { isObjectLike, type PhpArrayLike, type PhpAssoc, toPhpArrayObject } from '../_helpers/_phpTypes.ts'

export function array_replace<TValue>(
arr: PhpArrayLike<TValue>,
firstReplacement: PhpArrayLike<TValue> | null | undefined,
...additionalReplacements: Array<PhpArrayLike<TValue> | null | undefined>
): PhpAssoc<TValue | undefined> {
// discuss at: https://locutus.io/php/array_replace/
// original by: Brett Zamir (https://brett-zamir.me)
// example 1: array_replace(["orange", "banana", "apple", "raspberry"], {0 : "pineapple", 4 : "cherry"}, {0:"grape"})
// returns 1: {0: 'grape', 1: 'banana', 2: 'apple', 3: 'raspberry', 4: 'cherry'}

const retObj: PhpAssoc<TValue | undefined> = {}

// 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
}

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