PHP's array_search in TypeScript

✓ Verified: PHP 8.3
Examples tested against actual runtime. CI re-verifies continuously. Only documented examples are tested.

How to use

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

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

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
1array_search('zonneveld', {firstname: 'kevin', middle: 'van', surname: 'zonneveld'})'surname'
2array_search('3', {a: 3, b: 5, c: 7})'a'

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

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

type SearchValue = PhpRuntimeValue

export function array_search<TValue extends SearchValue>(
needle: TValue | RegExp,
haystack: PhpAssoc<TValue>,
argStrict: true,
): string | false

export function array_search(
needle: SearchValue | RegExp,
haystack: PhpAssoc<SearchValue>,
argStrict?: boolean,
): string | false

export function array_search(
needle: SearchValue | RegExp,
haystack: PhpAssoc<SearchValue>,
argStrict?: boolean,
): string | false {
// discuss at: https://locutus.io/php/array_search/
// parity verified: PHP 8.3
// original by: Kevin van Zonneveld (https://kvz.io)
// input by: Brett Zamir (https://brett-zamir.me)
// bugfixed by: Kevin van Zonneveld (https://kvz.io)
// bugfixed by: Reynier de la Rosa (https://scriptinside.blogspot.com.es/)
// example 1: array_search('zonneveld', {firstname: 'kevin', middle: 'van', surname: 'zonneveld'})
// returns 1: 'surname'
// example 2: array_search('3', {a: 3, b: 5, c: 7})
// returns 2: 'a'

const strict = Boolean(argStrict)

if (needle instanceof RegExp) {
// Duck-type for RegExp
let regex: RegExp = needle
if (!strict) {
// Let's consider case sensitive searches as strict
const flags =
'i' +
(needle.global ? 'g' : '') +
(needle.multiline ? 'm' : '') +
// sticky is FF only
(needle.sticky ? 'y' : '')
regex = new RegExp(needle.source, flags)
}
for (const [key, value] of Object.entries(haystack)) {
if (regex.test(String(value))) {
return key
}
}
return false
}

for (const [key, value] of Object.entries(haystack)) {
// biome-ignore lint/suspicious/noDoubleEquals: non-strict comparison intended
if ((strict && value === needle) || (!strict && value == needle)) {
return key
}
}

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