Table of Contents
in_array() function
array_search() function
isset() and array_key_exists() functions
Conclusion
Home Backend Development PHP Problem How to determine whether a certain value exists in an array in php

How to determine whether a certain value exists in an array in php

Apr 26, 2023 am 09:10 AM

As a programming language widely used in web development, PHP provides a wealth of functions and methods to handle various data types, including arrays. In the use of arrays, it is a common requirement to determine whether a certain value exists in the array. So, how to implement this function in PHP? This article will introduce you to several commonly used methods.

in_array() function

PHP provides an in_array() function, which can easily determine whether a value exists in an array. The prototype of this function is as follows:

1

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

Copy after login

Among them, $needle represents the value to be searched, $haystack represents the array to be searched, and $strict represents whether to use strict mode. The default is false. If the value is found, return true; otherwise, return false.

For example, we have an array $fruits with the content ["apple", "banana", "pineapple", "orange"]. Now to find whether the value "banana" exists in it, you can use in_array () function, the code is as follows:

1

2

3

4

5

6

$fruits array("apple""banana""pineapple""orange");

if (in_array("banana"$fruits)) {

    echo "该值已存在于数组中";

else {

    echo "该值不存在于数组中";

}

Copy after login

As can be seen from the above code, if the value "banana" exists, it will output "The value already exists in the array"; otherwise, it will output "The value does not exist" in the array".

It should be noted that the in_array() function uses non-strict mode by default, that is to say, if the value searched is inconsistent with the data type of an element in the array, it will also be considered to exist. If you want to use strict mode, you need to set the $strict parameter to true. For example:

1

2

3

4

5

6

$numbers array(1, 2, 3, "4");

if (in_array("4"$numbers, true)) {

    echo "该值已存在于数组中";

else {

    echo "该值不存在于数组中";

}

Copy after login

As can be seen from the above code, although there is an element in the array with a value of 4, because it is a string type and the value to be found is a numeric type, it is not in strict mode. The following will be considered as non-existent. If the $strict parameter is set to true, strict mode will be used to search, and "the value does not exist in the array" will be output.

array_search() function

Similar to the in_array() function, PHP also provides another function array_search(), which can find a value in an array and return the subscript of the value. . The prototype of this function is as follows:

1

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

Copy after login

Among them, $needle represents the value to be searched, $haystack represents the array to be searched, and $strict represents whether to use strict mode. The default is false. If the value is found, the subscript at which the value is located is returned; otherwise, false is returned.

For example, we have an array $numbers with the content [1, 2, 3, 4, 5]. Now we want to find whether there is an element with a value of 4 and output the subscript of the element. You can Using the array_search() function, the code is as follows:

1

2

3

4

5

6

$numbers array(1, 2, 3, 4, 5);

if (($key array_search(4, $numbers)) !== false) {

    echo "该值存在于数组中,其下标为" $key;

else {

    echo "该值不存在于数组中";

}

Copy after login

As can be seen from the above code, if an element with a value of 4 is found, "This value exists in the array, and its subscript is 3"; otherwise , output "The value does not exist in the array".

It should also be noted that the array_search() function will also use non-strict mode to search. If you want to use strict mode, you need to set the $strict parameter to true. For example:

1

2

3

4

5

6

$numbers array(1, 2, 3, "4", 5);

if (($key array_search("4"$numbers, true)) !== false) {

    echo "该值存在于数组中,其下标为" $key;

else {

    echo "该值不存在于数组中";

}

Copy after login

As you can see from the above code, in non-strict mode, you can find the element with the value "4" and output "This value exists in the array, and its subscript is 3". In strict mode, since the value to be found is of string type and the value of the element in the array is of numeric type and cannot be matched, "the value does not exist in the array" will be output.

isset() and array_key_exists() functions

In addition to the above two functions, you can also use the isset() and array_key_exists() functions to determine whether a key or subscript exists in the array .

isset() function can detect whether a variable has been set and is not null. If the variable has been set and is not null, returns true; otherwise, returns false. When checking whether a key exists in an array, you can use the isset() function. For example:

1

2

3

4

5

6

$person array("name" => "Tom""age" => 20);

if (isset($person["name"])) {

    echo "该键存在于数组中";

else {

    echo "该键不存在于数组中";

}

Copy after login

As can be seen from the above code, if there is an element with the key "name", it will output "the key exists in the array"; otherwise, it will output "the key does not exist in the array" .

It should be noted that when the isset() function detects non-existent array elements, it will return false without throwing a warning. For example:

1

2

3

4

5

6

$numbers array(1, 2, 3, 4, 5);

if (isset($numbers[5])) {

    echo "该下标存在于数组中";

else {

    echo "该下标不存在于数组中";

}

Copy after login

As can be seen from the above code, since the element with subscript 5 does not exist in the array, "The subscript does not exist in the array" will be output.

Similar to the isset() function, the array_key_exists() function can also detect whether a key exists in an array. The prototype of this function is as follows:

1

bool array_key_exists ( mixed $key array $array )

Copy after login

Among them, $key represents the key to be searched, and $array represents the array to be searched. If the key is found, returns true; otherwise, returns false.

For example, we have an array $person with the content ["name" => "Tom", "age" => 20]. Now we want to find whether there is an element with the key "name" in it. , and output whether it exists, you can use the array_key_exists() function, the code is as follows:

1

2

3

4

5

6

$person array("name" => "Tom""age" => 20);

if (array_key_exists("name"$person)) {

    echo "该键存在于数组中";

else {

    echo "该键不存在于数组中";

}

Copy after login

As can be seen from the above code, if there is an element with the key "name", it will output "The key exists in the array" "; Otherwise, output "The key does not exist in the array".

Conclusion

In short, judging whether a certain value or key exists in an array is a common operation in PHP, and it is also easy to implement. In addition to the methods introduced above, there are many other functions and methods that can achieve this function. When using it, you can choose the most suitable method according to the actual situation to improve the efficiency and readability of the program.

The above is the detailed content of How to determine whether a certain value exists in an array in php. 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 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)

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities. OWASP Top 10 PHP: Describe and mitigate common vulnerabilities. Mar 26, 2025 pm 04:13 PM

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

PHP 8 JIT (Just-In-Time) Compilation: How it improves performance. PHP 8 JIT (Just-In-Time) Compilation: How it improves performance. Mar 25, 2025 am 10:37 AM

PHP 8's JIT compilation enhances performance by compiling frequently executed code into machine code, benefiting applications with heavy computations and reducing execution times.

PHP Secure File Uploads: Preventing file-related vulnerabilities. PHP Secure File Uploads: Preventing file-related vulnerabilities. Mar 26, 2025 pm 04:18 PM

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

PHP Encryption: Symmetric vs. asymmetric encryption. PHP Encryption: Symmetric vs. asymmetric encryption. Mar 25, 2025 pm 03:12 PM

The article discusses symmetric and asymmetric encryption in PHP, comparing their suitability, performance, and security differences. Symmetric encryption is faster and suited for bulk data, while asymmetric is used for secure key exchange.

PHP Authentication & Authorization: Secure implementation. PHP Authentication & Authorization: Secure implementation. Mar 25, 2025 pm 03:06 PM

The article discusses implementing robust authentication and authorization in PHP to prevent unauthorized access, detailing best practices and recommending security-enhancing tools.

PHP API Rate Limiting: Implementation strategies. PHP API Rate Limiting: Implementation strategies. Mar 26, 2025 pm 04:16 PM

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

PHP Input Validation: Best practices. PHP Input Validation: Best practices. Mar 26, 2025 pm 04:17 PM

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

PHP CSRF Protection: How to prevent CSRF attacks. PHP CSRF Protection: How to prevent CSRF attacks. Mar 25, 2025 pm 03:05 PM

The article discusses strategies to prevent CSRF attacks in PHP, including using CSRF tokens, Same-Site cookies, and proper session management.

See all articles