Home  >  Article  >  Backend Development  >  How to determine if a variable exists in an array in php

How to determine if a variable exists in an array in php

PHPz
PHPzOriginal
2023-04-23 16:43:54622browse

In PHP, array is a very common data structure. Many times, we may need to check whether an element exists in an array. In PHP, there are multiple ways to check if a certain variable exists in an array. This article will introduce some of the more commonly used methods.

Method 1: in_array()

in_array() is one of the built-in functions in PHP, used to determine whether a value exists in an array. Its syntax structure is as follows:

bool in_array(mixed $needle, array $haystack [, bool $strict = FALSE])

Among them, $needle represents the value we want to find, $haystack Indicates the array we want to query in. $strict indicates whether strict mode is turned on. The default is false. The return value is of bool type. A value of true indicates that the element exists in the array, otherwise it does not exist.

The following is an example of using the in_array() function:

$fruits = array('apple','banana','orange','pear');
if ( in_array('apple', $fruits)) {

echo "apple is in the array";

} else {

echo "apple is not in the array";

}

The output result is: apple is in the array

Method 2: array_key_exists()

array_key_exists() is also one of the built-in functions in PHP, used to check whether a specified key name exists in an array. Its syntax structure is as follows:

bool array_key_exists(mixed $key, array $array)

Among them, $key represents the key name we want to find, and $array represents the key we want to query. array. The return value is of bool type. A value of true indicates that the key name exists in the array, otherwise it does not exist.

The following is an example of using the array_key_exists() function:

$person = array('name' => 'Tom', 'age' => 18, 'gender' = > 'male');
if (array_key_exists('name', $person)) {

echo "name is a key in the array";

} else {

echo "name is not a key in the array";

}

The output result is :name is a key in the array

Method 3: isset()

isset() function is one of the built-in functions in PHP, used to detect whether the variable has been set and is not null. Its syntax structure is as follows:

bool isset(mixed $var [, mixed $... ])

Among them, $var represents the variable we want to detect, and multiple variables can be detected at the same time . The return value is of bool type. A value of true indicates that the variable has been defined and is not null, otherwise it is false.

For arrays, we can use isset() to check whether a certain key or value exists. The following is an example of using the isset() function:

$person = array('name' => 'Tom', 'age' => 18, 'gender' => 'male') ;
if (isset($person['name'])) {

echo "name is a key in the array";

} else {

echo "name is not a key in the array";

}

The output result is: name is a key in the array

Method 4: array_search()

array_search() is one of the built-in functions in PHP, used to find the specified value in the array and return its location. Its syntax structure is as follows:

mixed array_search(mixed $needle, array $haystack [, bool $strict = FALSE])

Among them, $needle represents the value we want to find, $haystack Indicates the array we want to query in. $strict indicates whether strict mode is turned on. The default is false. The return value is of mixed type. If it exists, its key name in the array is returned, otherwise false is returned.

The following is an example of using the array_search() function:

$fruits = array('apple','banana','orange','pear');
$search_key = array_search('orange', $fruits);
if ($search_key !== false) {

echo "orange is in the array, and its key is " . $search_key;

} else {

echo "orange is not in the array";

}

Output results For: orange is in the array, and its key is 2

To sum up, we can use a variety of methods in PHP to check whether a specified value or key name exists in an array. Just choose the appropriate method according to actual needs.

The above is the detailed content of How to determine if a variable exists in an array in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn