PHP's array_replace_recursive 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_replace_recursive } from 'locutus/php/array/array_replace_recursive'.

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

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_recursive({'citrus' : ['orange'], 'berries' : ['blackberry', 'raspberry']}, {'citrus' : ['pineapple'], 'berries' : ['blueberry']}){citrus : ['pineapple'], berries : ['blueberry', 'raspberry']}

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_recursive looks like.

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

interface RecursiveReplaceObject extends PhpAssoc<RecursiveReplaceValue> {}
type RecursiveReplaceValue = PhpRuntimeValue | RecursiveReplaceValue[] | RecursiveReplaceObject
type RecursiveReplaceTarget = RecursiveReplaceValue[] | RecursiveReplaceObject

const cloneReplaceTarget = (value: RecursiveReplaceTarget): RecursiveReplaceTarget => {
if (Array.isArray(value)) {
return [...value]
}

return { ...value }
}

export function array_replace_recursive(
arr: RecursiveReplaceTarget,
...replacements: [replacement: RecursiveReplaceTarget, ...additionalReplacements: RecursiveReplaceTarget[]]
): RecursiveReplaceTarget {
// discuss at: https://locutus.io/php/array_replace_recursive/
// parity verified: PHP 8.3
// original by: Brett Zamir (https://brett-zamir.me)
// example 1: array_replace_recursive({'citrus' : ['orange'], 'berries' : ['blackberry', 'raspberry']}, {'citrus' : ['pineapple'], 'berries' : ['blueberry']})
// returns 1: {citrus : ['pineapple'], berries : ['blueberry', 'raspberry']}

if (replacements.length < 1) {
throw new Error('There should be at least 2 arguments passed to array_replace_recursive()')
}

// Although docs state that the arguments are passed in by reference,
// it seems they are not altered, but rather the copy that is returned
// So we make a copy here, instead of acting on arr itself
const retObj = cloneReplaceTarget(arr)
const retObjLike = toPhpArrayObject<RecursiveReplaceValue>(retObj)
for (const replacement of replacements) {
const replacementObj = toPhpArrayObject<RecursiveReplaceValue>(replacement)
for (const p in replacementObj) {
if (isObjectLike(retObjLike[p]) && isObjectLike(replacementObj[p])) {
retObjLike[p] = array_replace_recursive(
toPhpArrayObject<RecursiveReplaceValue>(retObjLike[p]),
toPhpArrayObject<RecursiveReplaceValue>(replacementObj[p]),
)
} else {
retObjLike[p] = replacementObj[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