PHP's array_intersect_key in TypeScript

How to use

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

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

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', 0: 'yellow', 1: 'red'} array_intersect_key($array1, $array2){0: 'red', a: 'green'}

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.

Notes

  • These only output associative arrays (would need to be all numeric and counting from zero to be numeric)

Dependencies

This function uses the following Locutus functions:

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

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

export function array_intersect_key<TValue>(
arr1: PhpArrayLike<TValue>,
...arrays: Array<PhpArrayLike<TValue>>
): PhpAssoc<TValue> {
// discuss at: https://locutus.io/php/array_intersect_key/
// original by: Brett Zamir (https://brett-zamir.me)
// note 1: These only output associative arrays (would need to be
// note 1: all numeric and counting from zero to be numeric)
// example 1: var $array1 = {a: 'green', b: 'brown', c: 'blue', 0: 'red'}
// example 1: var $array2 = {a: 'green', 0: 'yellow', 1: 'red'}
// example 1: array_intersect_key($array1, $array2)
// returns 1: {0: 'red', a: 'green'}

const retArr: PhpAssoc<TValue> = {}
if (arrays.length < 1) {
return retArr
}

const arr1Object = toPhpArrayObject<TValue>(arr1)
const keySets = arrays.map((nextArray) => new Set(Object.keys(toPhpArrayObject<TValue>(nextArray))))

for (const [k1, arr1Value] of Object.entries(arr1Object)) {
if (keySets.every((keys) => keys.has(k1))) {
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