Php check if array element exists

(PHP 4 >= 4.0.7, PHP 5, PHP 7, PHP 8)

array_key_existsChecks if the given key or index exists in the array

Description

array_key_exists(string|int $key, array $array): bool

Parameters

key

Value to check.

array

An array with keys to check.

Return Values

Returns true on success or false on failure.

Note:

array_key_exists() will search for the keys in the first dimension only. Nested keys in multidimensional arrays will not be found.

Examples

Example #1 array_key_exists() example

$search_array = array('first' => 1'second' => 4);
if (
array_key_exists('first'$search_array)) {
    echo 
"The 'first' element is in the array";
}
?>

Example #2 array_key_exists() vs isset()

isset() does not return true for array keys that correspond to a null value, while array_key_exists() does.

$search_array = array('first' => null'second' => 4);// returns false
isset($search_array['first']);// returns true
array_key_exists('first'$search_array);
?>

Notes

Note:

For backward compatibility reasons, array_key_exists() will also return true if key is a property defined within an object given as array. This behaviour is deprecated as of PHP 7.4.0, and removed as of PHP 8.0.0.

To check whether a property exists in an object, property_exists() should be used.

See Also

  • isset() - Determine if a variable is declared and is different than null
  • array_keys() - Return all the keys or a subset of the keys of an array
  • in_array() - Checks if a value exists in an array
  • property_exists() - Checks if the object or class has a property

manhon824 at gmail dot com

11 years ago

I took hours for me to debug, and I finally recognized that,

You have to reset the $array before using array_key_exists
reset($array);
array_key_exists($needle,$array);

Or you will get no reply.

(PHP 4, PHP 5, PHP 7, PHP 8)

in_arrayChecks if a value exists in an array

Description

in_array(mixed $needle, array $haystack, bool $strict = false): bool

Parameters

needle

The searched value.

Note:

If needle is a string, the comparison is done in a case-sensitive manner.

haystack

The array.

strict

If the third parameter strict is set to true then the in_array() function will also check the types of the needle in the haystack.

Note:

Prior to PHP 8.0.0, a string needle will match an array value of 0 in non-strict mode, and vice versa. That may lead to undesireable results. Similar edge cases exist for other types, as well. If not absolutely certain of the types of values involved, always use the strict flag to avoid unexpected behavior.

Return Values

Returns true if needle is found in the array, false otherwise.

Examples

Example #1 in_array() example

$os = array("Mac""NT""Irix""Linux");
if (
in_array("Irix"$os)) {
    echo 
"Got Irix";
}
if (
in_array("mac"$os)) {
    echo 
"Got mac";
}
?>

The second condition fails because in_array() is case-sensitive, so the program above will display:

Example #2 in_array() with strict example

$a = array('1.10'12.41.13);

if (

in_array('12.4'$atrue)) {
    echo 
"'12.4' found with strict check\n";
}

if (

in_array(1.13$atrue)) {
    echo 
"1.13 found with strict check\n";
}
?>

The above example will output:

1.13 found with strict check

Example #3 in_array() with an array as needle

$a = array(array('p''h'), array('p''r'), 'o');

if (

in_array(array('p''h'), $a)) {
    echo 
"'ph' was found\n";
}

if (

in_array(array('f''i'), $a)) {
    echo 
"'fi' was found\n";
}

if (

in_array('o'$a)) {
    echo 
"'o' was found\n";
}
?>

The above example will output:

  'ph' was found
  'o' was found

See Also

  • array_search() - Searches the array for a given value and returns the first corresponding key if successful
  • isset() - Determine if a variable is declared and is different than null
  • array_key_exists() - Checks if the given key or index exists in the array

beingmrkenny at gmail dot com

10 years ago

Loose checking returns some crazy, counter-intuitive results when used with certain arrays. It is completely correct behaviour, due to PHP's leniency on variable types, but in "real-life" is almost useless.

The solution is to use the strict checking option.

// Example array$array = array(
   
'egg' => true,
   
'cheese' => false,
   
'hair' => 765,
   
'goblins' => null,
   
'ogres' => 'no ogres allowed in this array'
);// Loose checking -- return values are in comments

// First three make sense, last four do not

in_array(null, $array); // true
in_array(false, $array); // true
in_array(765, $array); // true
in_array(763, $array); // true
in_array('egg', $array); // true
in_array('hhh', $array); // true
in_array(array(), $array); // true

// Strict checking

in_array(null, $array, true); // true
in_array(false, $array, true); // true
in_array(765, $array, true); // true
in_array(763, $array, true); // false
in_array('egg', $array, true); // false
in_array('hhh', $array, true); // false
in_array(array(), $array, true); // false?>

rhill at xenu-directory dot net

13 years ago

I found out that in_array will *not* find an associative array within a haystack of associative arrays in strict mode if the keys were not generated in the *same order*:

$needle

= array(
   
'fruit'=>'banana', 'vegetable'=>'carrot'
   
);$haystack = array(
    array(
'vegetable'=>'carrot', 'fruit'=>'banana'),
    array(
'fruit'=>'apple', 'vegetable'=>'celery')
    );

echo

in_array($needle, $haystack, true) ? 'true' : 'false';
// Output is 'false'echo in_array($needle, $haystack) ? 'true' : 'false';
// Output is 'true'?>

I had wrongly assumed the order of the items in an associative array were irrelevant, regardless of whether 'strict' is TRUE or FALSE: The order is irrelevant *only* if not in strict mode.

thomas dot sahlin at gmail dot com

12 years ago

If you're creating an array yourself and then using in_array to search it, consider setting the keys of the array and using isset instead since it's much faster.

$slow

= array('apple', 'banana', 'orange');

if (

in_array('banana', $slow))
    print(
'Found it!');$fast = array('apple' => 'apple', 'banana' => 'banana', 'orange' => 'orange');

if (isset(

$fast['banana']))
    print(
'Found it!');?>

How do you check if a value exists in an array?

The simplest and fastest way to check if an item is present in an array is by using the Array. indexOf() method. This method searches the array for the given item and returns its index. If no item is found, it returns -1.

What is In_array function in PHP?

The in_array() function searches an array for a specific value. Note: If the search parameter is a string and the type parameter is set to TRUE, the search is case-sensitive.

What is use of count () function in PHP?

The count() function returns the number of elements in an array.

How can I check if two arrays contain any common item in PHP?

The array_intersect() function compares the values of two (or more) arrays, and returns the matches. This function compares the values of two or more arrays, and return an array that contains the entries from array1 that are present in array2, array3, etc.