Home  >  Article  >  Backend Development  >  How to determine whether it is in an array in php

How to determine whether it is in an array in php

PHPz
PHPzOriginal
2023-04-23 09:11:39392browse

PHP is a scripting language that is very suitable for interacting with various databases, processing and generating HTML web pages, etc. It is a very important part of Web development. In PHP, determining whether a value appears in an array is a common task. This article will introduce how to use PHP to determine whether a value appears in an array, and elaborate on and explain the various situations that may occur.

  1. in_array() function

PHP provides an in_array() function, which checks whether a value appears in an array. The syntax of this function is as follows:

in_array($value, $array, $strict = false)

Among them, $value is the value to be found, $array is the array to be found, and $strict is an optional parameter used to specify whether to perform strict comparison. If $strict is true, not only will the values ​​be compared, but also the types will be compared, that is, whether the value is a string, an integer, a floating point number, or a Boolean value.

Example:

$array = array('apple', 'banana', 'orange');
if (in_array('apple', $array)) {
    echo '苹果出现在数组中';
} else {
    echo '苹果不在该数组中';
}

This example code first creates an array containing apples, bananas, and oranges, and then uses the in_array() function to check whether apples appear. Since it appears in the array, the output is "Apple appears in array".

  1. array_search() function

Another function used to search an array is the array_search() function, which returns the key that matches the specified value. Its syntax is as follows:

array_search($needle, $haystack, $strict = false)

Among them, $needle is the value to be found, $haystack is the array to be found, and $strict is an optional parameter used to specify whether to perform strict comparison.

Example:

$array = array('apple', 'banana', 'orange');
$key = array_search('banana', $array);
if ($key !== false) {
    echo '在数组中找到了“banana”,它的键是' . $key;
} else {
    echo '该数组中没有“banana”';
}

This example first creates an array containing apples, bananas and oranges. Then search for "banana" in the array and save the result in the $key variable. Since "banana" is found in the array, the output is "Found 'banana' in the array, its key is 1".

  1. Use foreach loop

If you need to determine whether a value appears in the array without knowing the array structure, you can use foreach loop to traverse the entire array, each time Compares whether the value in the array is equal to the target value.

Example:

$array = array('apple', 'banana', 'orange');
$search = 'banana';
foreach ($array as $value) {
    if ($value == $search) {
        echo '在数组中找到了' . $search;
        break;
    }
}

This example first creates an array containing apples, bananas and oranges, then uses a foreach loop to iterate through the array elements and compare with the $search variable. Since "banana" is found in the array, the output is "Banana found in array".

  1. Using the isset() function

isset() function can be used to check whether an array contains a specified key. Returns true if included; false otherwise.

Example:

$array = array('apple' => 0, 'banana' => 1, 'orange' => 2);
$search = 'banana';
if (isset($array[$search])) {
    echo '在数组中找到了' . $search;
} else {
    echo '该数组中没有' . $search;
}

This example creates an associative array and uses the isset() function to check whether it contains the key "banana". Since the $array array contains the "banana" key, the output is "Banana found in array".

To sum up, there are many ways to determine whether a value appears in an array in PHP, including using the in_array() function, array_search() function, foreach loop and isset() function. It needs to be based on the actual situation. Choose the appropriate method. These methods work not only for arrays of string type, but also for arrays of numeric and associative types. Proficiency in these techniques is one of the skills that every PHP programmer should master, which can improve code efficiency and readability and shorten debugging time.

The above is the detailed content of How to determine whether it is in an array 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