PHP's in_array in JavaScript

How to use

You you can install via yarn add locutus and require this function via const in_array = require('locutus/php/array/in_array').

It is important to use a bundler that supports tree-shaking so that you only ship the functions that you actually use to your browser, instead of all of Locutus, which is massive. Examples are: Parcel, webpack, or rollup.js. For server-side use this is typically less of a concern.

Examples

Please note that these examples are distilled from test cases that automatically verify our functions still work correctly. This could explain some quirky ones.

#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 JavaScript

Please note that Locutus uses 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 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.

So don't use this for anything serious if you rely on the order to be consistent accross platforms.

To influence how Locutus treats objects to arrays, you can check out the `locutus.objectsAsArrays` setting.

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

module.exports = function in_array(needle, haystack, argStrict) {
// 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

let key = ''
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 (strict) {
for (key in haystack) {
if (haystack[key] === needle) {
return true
}
}
} else {
for (key in haystack) {
// eslint-disable-next-line eqeqeq
if (haystack[key] == needle) {
return true
}
}
}

return false
}

A community effort

Not unlike Wikipedia, Locutus is an ongoing community effort. Our philosophy follows The McDonald’s Theory. This means that we assimilate first iterations with imperfections, hoping for others to take issue with-and improve them. This unorthodox approach has worked very well to foster fun and fruitful collaboration, but please be reminded to use our creations at your own risk. THE SOFTWARE IS PROVIDED "AS IS" has never been more true than for Locutus.

Now go and: [ View on GitHub | Edit on GitHub | View Raw ]


« More PHP array functions


Star