---
warning: This file is auto generated by `yarn web:inject`, do not edit by hand
examples:
- |-
var $array1 = {'a' : 'green', 0:'red', 1: 'blue'}
var $array2 = {'b' : 'green', 0:'yellow', 1:'red'}
var $array3 = ['green', 'red']
var $result = array_intersect($array1, $array2, $array3)
returns:
- '{0: ''red'', a: ''green''}'
dependencies:
- php/_helpers/_phpTypes
authors:
original by:
- Brett Zamir (https://brett-zamir.me)
notes:
- |-
These only output associative arrays (would need to be
all numeric and counting from zero to be numeric)
parityVerified: null
isTypescript: true
type: function
layout: function
title: PHP's array_intersect in TypeScript
description: >-
Here's what our current TypeScript equivalent to PHP's
array_intersect looks like.
function: array_intersect
category: array
language: php
permalink: php/array/array_intersect/
alias:
- /functions/php/array_intersect/
- /functions/array/array_intersect/
- /php/array_intersect/
- /functions/array_intersect/
---
{% codeblock lang:typescript %}import { entriesOfPhpAssoc, type PhpArrayLike, type PhpAssoc, toPhpArrayObject } from '../_helpers/_phpTypes.ts'
export function array_intersect
(
arr1: PhpArrayLike,
...arrays: Array>
): PhpAssoc {
// discuss at: https://locutus.io/php/array_intersect/
// 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', 0:'red', 1: 'blue'}
// example 1: var $array2 = {'b' : 'green', 0:'yellow', 1:'red'}
// example 1: var $array3 = ['green', 'red']
// example 1: var $result = array_intersect($array1, $array2, $array3)
// returns 1: {0: 'red', a: 'green'}
const retArr: PhpAssoc = {}
if (arrays.length < 1) {
return retArr
}
const arr1Object = toPhpArrayObject(arr1)
arr1keys: for (const [k1, arr1Value] of entriesOfPhpAssoc(arr1Object)) {
for (const nextArray of arrays) {
const arr = toPhpArrayObject(nextArray)
let found = false
for (const [, value] of entriesOfPhpAssoc(arr)) {
if (value === arr1Value) {
found = true
break
}
}
if (!found) {
continue arr1keys
}
}
retArr[k1] = arr1Value
}
return retArr
}
{% endcodeblock %}
{% codeblock lang:javascript %}import { entriesOfPhpAssoc, toPhpArrayObject } from '../_helpers/_phpTypes.ts'
export function array_intersect(arr1, ...arrays) {
// discuss at: https://locutus.io/php/array_intersect/
// 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', 0:'red', 1: 'blue'}
// example 1: var $array2 = {'b' : 'green', 0:'yellow', 1:'red'}
// example 1: var $array3 = ['green', 'red']
// example 1: var $result = array_intersect($array1, $array2, $array3)
// returns 1: {0: 'red', a: 'green'}
const retArr = {}
if (arrays.length < 1) {
return retArr
}
const arr1Object = toPhpArrayObject(arr1)
arr1keys: for (const [k1, arr1Value] of entriesOfPhpAssoc(arr1Object)) {
for (const nextArray of arrays) {
const arr = toPhpArrayObject(nextArray)
let found = false
for (const [, value] of entriesOfPhpAssoc(arr)) {
if (value === arr1Value) {
found = true
break
}
}
if (!found) {
continue arr1keys
}
}
retArr[k1] = arr1Value
}
return retArr
}{% endcodeblock %}
{% codeblock lang:typescript %}// php/_helpers/_phpTypes (Locutus helper dependency)
type PhpNullish = null | undefined
type PhpInput = {} | PhpNullish
type PhpList
= T[]
type PhpAssoc = { [key: string]: T }
type PhpArrayLike = PhpList | PhpAssoc
const entriesOfPhpAssoc = (value: PhpAssoc): Array<[string, T]> => {
return Object.entries(value)
}
function isObjectLike(value: PhpInput): value is PhpArrayLike {
return typeof value === 'object' && value !== null
}
function isPhpArrayObject(value: PhpInput): value is PhpAssoc {
return isObjectLike(value)
}
function toPhpArrayObject(value: PhpInput): PhpAssoc {
if (isPhpArrayObject(value)) {
return value
}
return {}
}
// php/array/array_intersect (target function module)
function array_intersect(
arr1: PhpArrayLike,
...arrays: Array>
): PhpAssoc {
// discuss at: https://locutus.io/php/array_intersect/
// 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', 0:'red', 1: 'blue'}
// example 1: var $array2 = {'b' : 'green', 0:'yellow', 1:'red'}
// example 1: var $array3 = ['green', 'red']
// example 1: var $result = array_intersect($array1, $array2, $array3)
// returns 1: {0: 'red', a: 'green'}
const retArr: PhpAssoc = {}
if (arrays.length < 1) {
return retArr
}
const arr1Object = toPhpArrayObject(arr1)
arr1keys: for (const [k1, arr1Value] of entriesOfPhpAssoc(arr1Object)) {
for (const nextArray of arrays) {
const arr = toPhpArrayObject(nextArray)
let found = false
for (const [, value] of entriesOfPhpAssoc(arr)) {
if (value === arr1Value) {
found = true
break
}
}
if (!found) {
continue arr1keys
}
}
retArr[k1] = arr1Value
}
return retArr
}{% endcodeblock %}
{% codeblock lang:javascript %}// php/_helpers/_phpTypes (Locutus helper dependency)
function isPhpArrayObject(value) {
return typeof value === 'object' && value !== null
}
function toPhpArrayObject(value) {
if (isPhpArrayObject(value)) {
return value
}
return {}
}
// php/array/array_intersect (target function module)
function array_intersect(arr1, ...arrays) {
// discuss at: https://locutus.io/php/array_intersect/
// 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', 0:'red', 1: 'blue'}
// example 1: var $array2 = {'b' : 'green', 0:'yellow', 1:'red'}
// example 1: var $array3 = ['green', 'red']
// example 1: var $result = array_intersect($array1, $array2, $array3)
// returns 1: {0: 'red', a: 'green'}
const retArr = {}
if (arrays.length < 1) {
return retArr
}
const arr1Object = toPhpArrayObject(arr1)
arr1keys: for (const [k1, arr1Value] of Object.entries(arr1Object)) {
for (const nextArray of arrays) {
const arr = toPhpArrayObject(nextArray)
let found = false
for (const [, value] of Object.entries(arr)) {
if (value === arr1Value) {
found = true
break
}
}
if (!found) {
continue arr1keys
}
}
retArr[k1] = arr1Value
}
return retArr
}{% endcodeblock %}