How to Check If an Array Value Exists

How can I check if an array element exists?

You can use either the language construct isset, or the function array_key_exists.

isset should be a bit faster (as it's not a function), but will return false if the element exists and has the value NULL.



For example, considering this array :

$a = array(
123 => 'glop',
456 => null,
);

And those three tests, relying on isset :

var_dump(isset($a[123]));
var_dump(isset($a[456]));
var_dump(isset($a[789]));

The first one will get you (the element exists, and is not null) :

boolean true

While the second one will get you (the element exists, but is null) :

boolean false

And the last one will get you (the element doesn't exist) :

boolean false



On the other hand, using array_key_exists like this :

var_dump(array_key_exists(123, $a));
var_dump(array_key_exists(456, $a));
var_dump(array_key_exists(789, $a));

You'd get those outputs :

boolean true
boolean true
boolean false

Because, in the two first cases, the element exists -- even if it's null in the second case. And, of course, in the third case, it doesn't exist.



For situations such as yours, I generally use isset, considering I'm never in the second case... But choosing which one to use is now up to you ;-)

For instance, your code could become something like this :

if (!isset(self::$instances[$instanceKey])) {
$instances[$instanceKey] = $theInstance;
}

How to check if an array value exists?

Using the instruction if?

if(isset($something['say']) && $something['say'] === 'bla') {
// do something
}

By the way, you are assigning a value with the key say twice, hence your array will result in an array with only one value.

How to check if a value exists in an array and get the next value?

  let x = ["", "comp", "myval", "view", "1"];
if (x.indexOf(yourVal) >= 0) {

let nextValue = x[x.indexOf(yourVal) + 1];

} else {
// doesn't exist.
}

Note : you won't get next values if your values is last value of array.

Check if value exists in nested array

You need to check occupation as well with Array#includes.

const occupationExists = value => users.some(user =>
user.occupation.includes(value)
);

how to check whether a value exists in array in javascript?

indexOf() compares searchElement to elements of the Array using strict
equality (the same method used by the === or triple-equals operator).

$("#add-employeeId").val() is returning string value and you are comparing it to int values.

Your function should be:

function isInArray(array, search)
{
return array.indexOf(parseInt(search)) >= 0;
}

Check if an element is present in an array

ECMAScript 2016 incorporates an includes() method for arrays that specifically solves the problem, and so is now the preferred method.

[1, 2, 3].includes(2);     // true
[1, 2, 3].includes(4); // false
[1, 2, 3].includes(1, 2); // false (second parameter is the index position in this array at which to begin searching)

As of JULY 2018, this has been implemented in almost all major browsers, if you need to support an older browser a polyfill is available.

Edit: Note that this returns false if the item in the array is an object. This is because similar objects are two different objects in JavaScript.

How do I check if an array includes a value in JavaScript?

Modern browsers have Array#includes, which does exactly that and is widely supported by everyone except IE: