PHP's in_array in TypeScript

How to use

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

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

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
1in_array('van', ['Kevin', 'van', 'Zonneveld'])true
2in_array('vlado', {0: 'Kevin', vlado: 'van', 1: 'Zonneveld'})false
3in_array(1, ['1', '2', '3']) in_array(1, ['1', '2', '3'], false)true true
4in_array(1, ['1', '2', '3'], true)false

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.

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

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

type InArrayValue = PhpRuntimeValue

export function in_array<TNeedle extends InArrayValue>(
needle: TNeedle,
haystack: PhpArrayLike<TNeedle>,
argStrict: true,
): boolean

export function in_array(needle: InArrayValue, haystack: PhpArrayLike<InArrayValue>, argStrict?: boolean): boolean

export function in_array(needle: InArrayValue, haystack: PhpArrayLike<InArrayValue>, argStrict?: boolean): boolean {
// discuss at: https://locutus.io/php/in_array/
// original by: Kevin van Zonneveld (https://kvz.io)
// improved by: vlado houba
// improved by: Jonas Sciangula Street (Joni2Back)
// input by: Billy
// bugfixed by: Brett Zamir (https://brett-zamir.me)
// example 1: in_array('van', ['Kevin', 'van', 'Zonneveld'])
// returns 1: true
// example 2: in_array('vlado', {0: 'Kevin', vlado: 'van', 1: 'Zonneveld'})
// returns 2: false
// example 3: in_array(1, ['1', '2', '3'])
// example 3: in_array(1, ['1', '2', '3'], false)
// returns 3: true
// returns 3: true
// example 4: in_array(1, ['1', '2', '3'], true)
// returns 4: false

const strict = !!argStrict

// we prevent the double check (strict && arr[key] === ndl) || (!strict && arr[key] === ndl)
// in just one for, in order to improve the performance
// deciding wich type of comparation will do before walk array
if (Array.isArray(haystack)) {
if (strict) {
for (const key in haystack) {
if (haystack[Number(key)] === needle) {
return true
}
}
} else {
for (const key in haystack) {
// biome-ignore lint/suspicious/noDoubleEquals: non-strict comparison intended
if (haystack[Number(key)] == needle) {
return true
}
}
}
} else if (strict) {
for (const key in haystack) {
if (haystack[key] === needle) {
return true
}
}
} else {
for (const key in haystack) {
// biome-ignore lint/suspicious/noDoubleEquals: non-strict comparison intended
if (haystack[key] == needle) {
return true
}
}
}

return false
}

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