PHP's array_diff_ukey in TypeScript

How to use

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

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

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 $array1 = {blue: 1, red: 2, green: 3, purple: 4} var $array2 = {green: 5, blue: 6, yellow: 7, cyan: 8} array_diff_ukey($array1, $array2, function (key1, key2){ return (key1 === key2 ? 0 : (key1 > key2 ? 1 : -1)); }){red: 2, purple: 4}

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

import { resolveNumericComparator } from '../_helpers/_callbackResolver.ts'
import {
entriesOfPhpAssoc,
isPhpCallableDescriptor,
type PhpAssoc,
type PhpKeyComparatorDescriptor,
toPhpArrayObject,
} from '../_helpers/_phpTypes.ts'

type DiffArray<T> = PhpAssoc<T> | T[]

export function array_diff_ukey<T>(
arr1: PhpAssoc<T>,
...arraysAndCallback: [arr2: DiffArray<T>, ...rest: Array<DiffArray<T>>, callback: PhpKeyComparatorDescriptor]
): PhpAssoc<T> {
// discuss at: https://locutus.io/php/array_diff_ukey/
// original by: Brett Zamir (https://brett-zamir.me)
// example 1: var $array1 = {blue: 1, red: 2, green: 3, purple: 4}
// example 1: var $array2 = {green: 5, blue: 6, yellow: 7, cyan: 8}
// example 1: array_diff_ukey($array1, $array2, function (key1, key2){ return (key1 === key2 ? 0 : (key1 > key2 ? 1 : -1)); })
// returns 1: {red: 2, purple: 4}

const retArr: PhpAssoc<T> = {}
const callback = arraysAndCallback.at(-1)
if (typeof callback === 'undefined' || !isPhpCallableDescriptor<[string, string]>(callback)) {
throw new Error('array_diff_ukey(): Invalid callback')
}
const arrays = arraysAndCallback.slice(0, -1).map((value) => toPhpArrayObject<T>(value))
const keyComparator = resolveNumericComparator<string, string>(callback, 'array_diff_ukey(): Invalid callback')

arr1keys: for (const [k1, arr1Value] of entriesOfPhpAssoc(arr1)) {
for (const arr of arrays) {
for (const [k] of entriesOfPhpAssoc(arr)) {
if (keyComparator(k, k1) === 0) {
// If it reaches here, it was found in at least one array, so try next value
continue arr1keys
}
}
}
retArr[k1] = arr1Value
}

return retArr
}

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