PHP's array_merge in TypeScript

How to use

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

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

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
1var $arr1 = {"color": "red", 0: 2, 1: 4} var $arr2 = {0: "a", 1: "b", "color": "green", "shape": "trapezoid", 2: 4} array_merge($arr1, $arr2){"color": "green", 0: 2, 1: 4, 2: "a", 3: "b", "shape": "trapezoid", 4: 4}
2var $arr1 = [] var $arr2 = {1: "data"} array_merge($arr1, $arr2){0: "data"}

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.

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

import type { PhpArrayLike, PhpAssoc, PhpInput, PhpList } from '../_helpers/_phpTypes.ts'

type AssociativeArray<T = PhpInput> = PhpAssoc<T>

export function array_merge<T>(...args: [first: PhpList<T>, ...rest: Array<PhpList<T>>]): PhpList<T>

export function array_merge<T>(...args: [first: PhpArrayLike<T>, ...rest: Array<PhpArrayLike<T>>]): AssociativeArray<T>

export function array_merge<T>(
...args: [first: PhpArrayLike<T>, ...rest: Array<PhpArrayLike<T>>]
): PhpList<T> | AssociativeArray<T> {
// discuss at: https://locutus.io/php/array_merge/
// original by: Brett Zamir (https://brett-zamir.me)
// bugfixed by: Nate
// bugfixed by: Brett Zamir (https://brett-zamir.me)
// input by: josh
// example 1: var $arr1 = {"color": "red", 0: 2, 1: 4}
// example 1: var $arr2 = {0: "a", 1: "b", "color": "green", "shape": "trapezoid", 2: 4}
// example 1: array_merge($arr1, $arr2)
// returns 1: {"color": "green", 0: 2, 1: 4, 2: "a", 3: "b", "shape": "trapezoid", 4: 4}
// example 2: var $arr1 = []
// example 2: var $arr2 = {1: "data"}
// example 2: array_merge($arr1, $arr2)
// returns 2: {0: "data"}

const retObj: AssociativeArray<T> = {}
let retArr = true

for (const arg of args) {
if (!Array.isArray(arg)) {
retArr = false
break
}
}

if (retArr) {
let merged: PhpList<T> = []
for (const arg of args) {
if (Array.isArray(arg)) {
merged = merged.concat(arg)
}
}
return merged
}

for (let ct = 0, i = 0; i < args.length; i++) {
const arg = args[i]
if (typeof arg === 'undefined') {
continue
}
if (Array.isArray(arg)) {
for (let j = 0, argil = arg.length; j < argil; j++) {
const value = arg[j]
if (typeof value !== 'undefined') {
retObj[ct++] = value
}
}
} else {
for (const [k, value] of Object.entries(arg)) {
if (typeof value === 'undefined') {
continue
}
if (parseInt(k, 10) + '' === k) {
retObj[ct++] = value
} else {
retObj[k] = value
}
}
}
}

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