PHP's array_uintersect_uassoc in TypeScript

How to use

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

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

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 = {a: 'green', b: 'brown', c: 'blue', 0: 'red'} var $array2 = {a: 'GREEN', B: 'brown', 0: 'yellow', 1: 'red'} array_uintersect_uassoc($array1, $array2, function (f_string1, f_string2){var string1 = (f_string1+'').toLowerCase(); var string2 = (f_string2+'').toLowerCase(); if (string1 > string2) return 1; if (string1 === string2) return 0; return -1;}, function (f_string1, f_string2){var string1 = (f_string1+'').toLowerCase(); var string2 = (f_string2+'').toLowerCase(); if (string1 > string2) return 1; if (string1 === string2) return 0; return -1;}){a: 'green', b: 'brown'}

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

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

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

export function array_uintersect_uassoc<T>(
arr1: PhpAssoc<T>,
...arraysAndComparators: [
arr2: IntersectArray<T>,
...rest: Array<IntersectArray<T>>,
valueCallback: PhpComparatorDescriptor<T>,
keyCallback: PhpKeyComparatorDescriptor,
]
): PhpAssoc<T> {
// discuss at: https://locutus.io/php/array_uintersect_uassoc/
// original by: Brett Zamir (https://brett-zamir.me)
// example 1: var $array1 = {a: 'green', b: 'brown', c: 'blue', 0: 'red'}
// example 1: var $array2 = {a: 'GREEN', B: 'brown', 0: 'yellow', 1: 'red'}
// example 1: array_uintersect_uassoc($array1, $array2, function (f_string1, f_string2){var string1 = (f_string1+'').toLowerCase(); var string2 = (f_string2+'').toLowerCase(); if (string1 > string2) return 1; if (string1 === string2) return 0; return -1;}, function (f_string1, f_string2){var string1 = (f_string1+'').toLowerCase(); var string2 = (f_string2+'').toLowerCase(); if (string1 > string2) return 1; if (string1 === string2) return 0; return -1;})
// returns 1: {a: 'green', b: 'brown'}

const retArr: PhpAssoc<T> = {}
const keyCallback = arraysAndComparators.at(-1)
const valueCallback = arraysAndComparators.at(-2)
if (
typeof keyCallback === 'undefined' ||
typeof valueCallback === 'undefined' ||
!isPhpCallableDescriptor<[string, string]>(keyCallback) ||
!isPhpCallableDescriptor<[T, T]>(valueCallback)
) {
throw new Error('array_uintersect_uassoc(): Invalid callback')
}
const arrays = arraysAndComparators.slice(0, -2).map((value) => toPhpArrayObject<T>(value))
const lastArrayIndex = arrays.length - 1
const keyComparator = resolveNumericComparator<string, string>(
keyCallback,
'array_uintersect_uassoc(): Invalid key callback',
)
const valueComparator = resolveNumericComparator<T, T>(
valueCallback,
'array_uintersect_uassoc(): Invalid value callback',
)

arr1keys: for (const [k1, arr1Value] of entriesOfPhpAssoc(arr1)) {
arrs: for (const [i, arr] of arrays.entries()) {
for (const [k, arrValue] of entriesOfPhpAssoc(arr)) {
if (valueComparator(arrValue, arr1Value) === 0 && keyComparator(k, k1) === 0) {
if (i === lastArrayIndex) {
retArr[k1] = arr1Value
}
// If the innermost loop always leads at least once to an equal value,
// continue the loop until done
continue arrs
}
}
// If it reaches here, it wasn't found in at least one array, so try next value
continue arr1keys
}
}

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