Home Backend Development PHP Problem Whether the php element exists in the array

Whether the php element exists in the array

May 19, 2023 pm 12:09 PM

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);
Copy after login

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 不存在于数组中";
  }
}
Copy after login

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);
Copy after login

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 不存在于数组中";
}
Copy after login

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);
Copy after login

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 不存在于数组中";
}
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24