Table of Contents
1. Use the in_array() function
2. Use the array_search() function
3. Use the array_key_exists() function
4. Use the isset() function
5. Summary
Home Backend Development PHP Problem PHP determines whether an array has a specified value

PHP determines whether an array has a specified value

May 11, 2023 am 09:08 AM

In PHP, it is a common operation to determine whether an array contains a specified value. Sometimes we need to search an array to determine whether a certain value exists, or we need to judge whether a specified key name exists in the array, etc. This article will introduce common methods and application scenarios in PHP to determine whether an array has a specified value.

1. Use the in_array() function

The in_array() function is one of the functions in PHP used to determine whether a specified value exists in an array. Its syntax is:

in_array($needle, $haystack, $strict)
Copy after login

This function accepts three parameters:

  • $needle: the value to be found
  • $haystack: to be found in Array of values
  • $strict: Optional parameter, if this parameter is set to true, a type comparison will be performed

The following is a sample code that uses the in_array() function to determine whether a specified value exists in an array:

$fruits = array("apple", "banana", "orange");
if (in_array("banana", $fruits)) {
    echo "存在";
} else {
    echo "不存在";
}
Copy after login

In the above example, we first defined an array containing the names of three fruits$fruits, and then use the in_array() function to check whether the element "banana" exists. The output of this example is exists.

2. Use the array_search() function

array_search() function is also one of the functions used to find array elements in PHP. This function returns a key whose value corresponds to the specified search value. If the value is not found, the function returns false. Its syntax is as follows:

array_search($needle, $haystack, $strict)
Copy after login

The function accepts three parameters:

  • $needle: The value to find
  • $ haystack: Array in which to look for values ​​
  • $strict: Optional parameter, if this parameter is set to true, a type comparison will be performed

The following is an example code that uses the array_search() function to determine whether a specified value exists in an array:

$fruits = array("apple", "banana", "orange");
$index = array_search("banana", $fruits);
if ($index !== false) {
    echo "存在,位置为:".$index;
} else {
    echo "不存在";
}
Copy after login

In the above example, we also defined a file containing three types of fruit names. The array $fruits, and then use the array_search() function to find the element "banana" and get the corresponding key name. If found, the output exists at position: 1.

It should be noted that the most significant difference between the array_search() function and the in_array() function is that the former returns the key name of the element, while the latter only returns the key name of the element. Returns true or false.

3. Use the array_key_exists() function

array_key_exists() The function is used to check whether the specified key exists in the array. The syntax of this function is as follows:

array_key_exists($key, $array)
Copy after login

This function accepts two parameters:

  • $key: The key name to be found
  • $array: The array in which to find the key name

The following is a sample code that uses the array_key_exists() function to determine whether the specified key name exists in the array:

$fruits = array("apple" => 1, "banana" => 2, "orange" => 3);
if (array_key_exists("banana", $fruits)) {
    echo "存在";
} else {
    echo "不存在";
}
Copy after login

In the above example, we defined an associative array containing three key-value pairs. Use the array_key_exists() function to find the key name "banana". The output of this example is exists.

4. Use the isset() function

isset() function is used to check whether the variable is set and not null. By using the isset() function in an array, we can check whether a key exists in the array. The syntax is as follows:

isset($array[$key])
Copy after login

It should be noted that the isset() function can only be used to check whether a variable is set, but cannot check whether the variable contains a value.

The following is an example code that uses the isset() function to determine whether the specified key name exists in the array:

$fruits = array("apple" => 1, "banana" => 2, "orange" => 3);
if (isset($fruits["banana"])) {
    echo "存在";
} else {
    echo "不存在";
}
Copy after login

In the above example, we also defined an association Array, use the isset() function to find the key name "banana". The output of this example is exists.

5. Summary

The above are some common ways to determine whether an array contains a specified value in PHP. According to actual needs, we can choose to use the in_array(), array_search(), array_key_exists() or isset() function to implement Different search operations. When using these functions, we need to understand the syntax and differences of different functions in order to use them more flexibly.

The above is the detailed content of PHP determines whether an array has a specified value. 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
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)

What are the best practices for deduplication of PHP arrays What are the best practices for deduplication of PHP arrays Mar 03, 2025 pm 04:41 PM

What are the best practices for deduplication of PHP arrays

Can PHP array deduplication take advantage of key name uniqueness? Can PHP array deduplication take advantage of key name uniqueness? Mar 03, 2025 pm 04:51 PM

Can PHP array deduplication take advantage of key name uniqueness?

Does PHP array deduplication need to be considered for performance losses? Does PHP array deduplication need to be considered for performance losses? Mar 03, 2025 pm 04:47 PM

Does PHP array deduplication need to be considered for performance losses?

What Are the Latest PHP Coding Standards and Best Practices? What Are the Latest PHP Coding Standards and Best Practices? Mar 10, 2025 pm 06:16 PM

What Are the Latest PHP Coding Standards and Best Practices?

What are the optimization techniques for deduplication of PHP arrays What are the optimization techniques for deduplication of PHP arrays Mar 03, 2025 pm 04:50 PM

What are the optimization techniques for deduplication of PHP arrays

How to Implement message queues (RabbitMQ, Redis) in PHP? How to Implement message queues (RabbitMQ, Redis) in PHP? Mar 10, 2025 pm 06:15 PM

How to Implement message queues (RabbitMQ, Redis) in PHP?

How Do I Work with PHP Extensions and PECL? How Do I Work with PHP Extensions and PECL? Mar 10, 2025 pm 06:12 PM

How Do I Work with PHP Extensions and PECL?

How to Use Reflection to Analyze and Manipulate PHP Code? How to Use Reflection to Analyze and Manipulate PHP Code? Mar 10, 2025 pm 06:12 PM

How to Use Reflection to Analyze and Manipulate PHP Code?

See all articles