Home  >  Article  >  Backend Development  >  Whether the php element exists in the array

Whether the php element exists in the array

王林
王林Original
2023-05-19 12:09:08531browse

In PHP, if we want to check whether an element exists in an array, we can use the in_array() function. This function accepts two parameters: the first is the element to be found, and the second is the array to search. The function returns true if the element exists in the array, false otherwise.

The syntax is as follows:

in_array($needle, $haystack);

Among them, $needle represents the element to be found, and $haystack represents the array to be searched.

For example, we can write a function to check whether a given number is in an array:

function checkNumber($num, $arr) {
  if (in_array($num, $arr)) {
    echo "$num 存在于数组中";
  } else {
    echo "$num 不存在于数组中";
  }
}

In this example, we pass in a number and an array as parameters, and then Use the in_array() function to check if a number is in an array. If the number exists in the array, the function prints "$num exists in array", otherwise it prints "$num does not exist in array".

In addition to the in_array() function, there are some other functions in PHP that can also be used to check whether an element exists in an array.

array_search() function can search the specified value in the array and return the key name. If the element does not exist, returns false.

The syntax is as follows:

array_search($needle, $haystack);

Among them, $needle represents the element to be searched, and $haystack represents the array to be found.

For example, we can use the following code to check if the number is in the array and get its key name:

$arr = [1, 2, 3, 4, 5];
$num = 3;
$key = array_search($num, $arr);
if ($key !== false) {
  echo "数字 $num 存在于数组中,键名为 $key";
} else {
  echo "数字 $num 不存在于数组中";
}

If the number exists in the array, the function will output "Number 3 exists in the array , the key name is 2". If the number does not exist in the array, the function will output "Number 3 does not exist in the array".

In addition to the in_array() and array_search() functions, we can also use the array_key_exists() function to check whether the specified key exists in the array.

array_key_exists() function accepts two parameters: the first is the key to find, and the second is the array to search. The function returns true if the specified key exists in the array, false otherwise.

The syntax is as follows:

array_key_exists($key, $array);

For example, we can use the following code to check if the key exists in the array:

$arr = ['foo' => 'bar', 'baz' => 'qux'];
$key = 'foo';
if (array_key_exists($key, $arr)) {
  echo "键 $key 存在于数组中,对应的值为 " . $arr[$key];
} else {
  echo "键 $key 不存在于数组中";
}

If the key exists in the array, the function will output "The key foo exists in the array and the corresponding value is bar". If the key does not exist in the array, the function will output "Key foo does not exist in the array".

In short, there are many methods in PHP to check whether an element exists in an array, and we can choose which method to use according to the actual situation.

The above is the detailed content of Whether the php element exists in the array. 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