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.
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:
exportfunction 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'}
exportfunctionarray_intersect_key(arr1, ...arrays) { // 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'}
functionisObjectLike(value: PhpInput): value is PhpArrayLike<PhpInput> { returntypeof value === 'object' && value !== null }
function isPhpArrayObject<T = PhpInput>(value: PhpInput): value is PhpAssoc<T> { returnisObjectLike(value) }
function toPhpArrayObject<T = PhpInput>(value: PhpInput): PhpAssoc<T> { if (isPhpArrayObject<T>(value)) { return value }
return {} }
// php/array/array_intersect_key (target function module) 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'}
for (const [k1, arr1Value] ofObject.entries(arr1Object)) { if (keySets.every((keys) => keys.has(k1))) { retArr[k1] = arr1Value } }
return retArr }
// php/_helpers/_phpTypes (Locutus helper dependency) functionisPhpArrayObject(value) { returntypeof value === 'object' && value !== null }
functiontoPhpArrayObject(value) { if (isPhpArrayObject(value)) { return value }
return {} }
// php/array/array_intersect_key (target function module) functionarray_intersect_key(arr1, ...arrays) { // 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'}
for (const [k1, arr1Value] ofObject.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.