Home  >  Article  >  Backend Development  >  How to check if an array exists in php

How to check if an array exists in php

PHPz
PHPzOriginal
2023-04-18 09:06:28704browse

In the field of programming, arrays are a very important data type and are often used to store the values ​​of multiple variables. When working with arrays, you often need to query whether a specific value exists in it. This article will introduce in detail how to use PHP language to query whether an array contains a certain value, and provide some examples to help readers understand better.

Part 1: The in_array() function in PHP

PHP provides a built-in function called in_array(), which can help us quickly detect whether an array exists the specified value.

The basic usage of the in_array() function is as follows:

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

Among them, $needle represents the value to be queried, $haystack represents the array to be queried, and $strict is an optional parameter, indicating Whether to use strict mode for comparison. If the strict argument is TRUE, the in_array() function compares both values ​​and types, otherwise only values ​​are compared.

In actual use, we can check whether an array contains a specified value through the following code:

$fruits = array('apple', 'banana', 'orange');
if (in_array('apple', $fruits)) {
    echo 'The fruit is found in the array';
}

In the above code, we define a $fruits array, and then use The in_array() function queries whether it contains the string 'apple'. If it exists, output 'The fruit is found in the array'.

Part 2: Using the array_search() function

In addition to the in_array() function, PHP also provides another built-in function called array_search(). This function can be used to find a specified value in an array and return its corresponding key name.

The basic usage of the array_search() function is as follows:

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

Among them, $needle represents the value to be queried, $haystack represents the array to be queried, and $strict is an optional parameter, indicating Whether to use strict mode for comparison. If the strict argument is TRUE, the array_search() function compares both values ​​and types, otherwise only values ​​are compared.

In actual use, we can use the following code to check whether an array contains a specified value and obtain the key name corresponding to the value:

$fruits = array('apple', 'banana', 'orange');
$key = array_search('banana', $fruits);
if ($key !== false) {
    echo 'The key of the fruit is ' . $key;
}

In the above code, we Define a $fruits array, and then use the array_search() function to query whether it contains the string 'banana'. If it exists, get the key name corresponding to the value and output 'The key of the fruit is '.

It should be noted that if the array_search() function does not find the corresponding key name, it will return FALSE. Therefore, to avoid errors, it is better to use the equals operator (===) to check both the type and the value when checking the return value.

Part 3: The difference between using in_array() and array_search() functions

Although both in_array() and array_search() functions can be used to check whether an array contains a specified value , but there are still some differences between them.

First of all, the in_array() function will only return a Boolean value indicating whether the specified value exists in the array. The array_search() function will return the key name corresponding to the value, or FALSE if the corresponding key name is not found.

Secondly, the in_array() function can only check the value, not the key name. The array_search() function can check the key name and value at the same time and return the key name corresponding to the value.

Therefore, in actual use, you need to choose to use the in_array() function or array_search() function according to specific needs.

Part 4: Example Demonstration

Some examples are provided below to help readers better understand how to use the in_array() and array_search() functions.

  • Example 1: Check whether an integer array contains the specified value

The following code demonstrates how to check whether an integer array contains the specified value:

$numbers = array(1, 2, 3, 4, 5);
if (in_array(3, $numbers)) {
    echo 'The value is found in the array';
}

In the above code, we define a $numbers array, and then use the in_array() function to check whether it contains the integer 3. If it exists, output 'The value is found in the array'.

  • Example 2: Check whether a string array contains the specified value and get its key name

The following code demonstrates how to check a string array Whether it contains the specified value and gets its key name:

$words = array('hello', 'world', 'php');
$key = array_search('php', $words);
if ($key !== false) {
    echo 'The key of the word is ' . $key;
}

In the above code, we define a $words array, and then use the array_search() function to check whether it contains the string 'php'. If it exists, get the key name corresponding to the string and output 'The key of the word is '.

  • Example 3: Find the specified value in a multi-dimensional array

The following code demonstrates how to find the specified value in a multi-dimensional array:

$fruits = array(
    array('name' => 'apple', 'color' => 'red'),
    array('name' => 'banana', 'color' => 'yellow'),
    array('name' => 'orange', 'color' => 'orange')
);
$fruit = 'banana';
$found = false;
foreach ($fruits as $key => $value) {
    if (in_array($fruit, $value)) {
        $found = true;
        echo 'The fruit is found in the array with key ' . $key;
        break;
    }
}
if (!$found) {
    echo 'The fruit is not found in the array';
}

In the above code, we define a $fruits multidimensional array, which contains several pieces of fruit information. Each fruit information includes two fields: name and color. We then use a foreach loop to loop through the entire array and use the in_array() function to find if the array contains the fruit name specified by $fruit. If the corresponding fruit is found, the key name of the fruit in the array is output. Otherwise, output 'The fruit is not found in the array'.

Part 5: Summary

This article introduces two methods in PHP to query whether a specified value exists in an array: in_array() and array_search() functions. In actual use, we must choose the appropriate method according to specific needs, and pay attention to using strict mode for type judgment. At the same time, we also provide some examples to help readers better master the use of these two methods. Learning these techniques can help developers query specific values ​​in arrays more quickly and improve code efficiency.

The above is the detailed content of How to check if an array exists 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