import type { PhpAssoc, PhpRuntimeValue } from '../_helpers/_phpTypes.ts' type SearchValue = PhpRuntimeValue export function array_search( needle: TValue | RegExp, haystack: PhpAssoc, argStrict: true, ): string | false export function array_search( needle: SearchValue | RegExp, haystack: PhpAssoc, argStrict?: boolean, ): string | false export function array_search( needle: SearchValue | RegExp, haystack: PhpAssoc, 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 }