Home  >  Article  >  Backend Development  >  How to determine whether a php array is greater than 80

How to determine whether a php array is greater than 80

PHPz
PHPzOriginal
2023-04-20 10:07:32527browse

PHP is a widely used server-side scripting language that is often used to create dynamic web pages and web applications. Array is a commonly used data type in PHP, which can store multiple values.

In PHP, we sometimes need to perform some filtering operations on the array, such as determining whether the number in the array is greater than 80. This article will introduce several methods to determine whether the number in the array is greater than 80.

Method 1: foreach loop

Use a foreach loop to traverse the array and determine whether the array elements are greater than 80 one by one. The code is as follows:

$nums = array(75, 90, 85, 55, 95);
$flag = true;
foreach ($nums as $num) {
    if ($num <= 80) {
        $flag = false;
        break;
    }
}

if ($flag) {
    echo '所有数字都大于80';
} else {
    echo '存在不大于80的数字';
}

First define a number containing multiple numbers The array $nums, and then defines a variable $flag with an initial value of true. Use a foreach loop to iterate through each element in the array $nums and assign it to the variable $num.

Each time through the loop, determine whether the variable $num is less than or equal to 80. If so, modify the variable $flag to false and use break to break out of the loop. Finally, the value of the variable $flag is judged. If it is true, it means that all numbers are greater than 80, otherwise it means that there are numbers not greater than 80.

Method 2: array_filter function

Use the array_filter function to filter out the elements less than or equal to 80 in the array, and then determine whether the filtered array is empty.

$nums = array(75, 90, 85, 55, 95);
$filtered_nums = array_filter($nums, function($value) {
    return $value <= 80;
});

if (empty($filtered_nums)) {
    echo '所有数字都大于80';
} else {
    echo '存在不大于80的数字';
}

First defines an array $nums containing multiple numbers. Use the array_filter function to traverse each element in the array $nums, filter out elements less than or equal to 80 based on the return value of the custom function, and store them in the new array $filtered_nums.

Finally determine whether the array $filtered_nums is empty. If it is empty, it means that all numbers are greater than 80, otherwise it means that there are numbers not greater than 80.

Method 3: array_reduce function

Use the array_reduce function to accumulate the elements in the array one by one. If there is an element less than or equal to 80, jump out of the loop directly and return false; if all elements are greater than 80 , then returns true.

$nums = array(75, 90, 85, 55, 95);
$flag = array_reduce($nums, function($carry, $item) {
    if ($item <= 80) {
        return false;
    }
    return $carry;
}, true);

if ($flag) {
    echo '所有数字都大于80';
} else {
    echo '存在不大于80的数字';
}

First defines an array $nums containing multiple numbers. Use the array_reduce function to accumulate elements in the array $nums one by one, and use a custom function to determine whether each element is greater than 80.

If there is an element less than or equal to 80, return false directly and jump out of the loop; if all elements are greater than 80, return true and continue executing the subsequent code.

Finally, determine the value of the variable $flag. If it is true, it means that all numbers are greater than 80, otherwise it means that there are numbers that are not greater than 80.

Summary:

This article introduces three methods to determine whether the number in a PHP array is greater than 80, namely: foreach loop, array_filter function and array_reduce function. Each of the three methods has its own characteristics, and readers can choose to use them according to specific circumstances.

The above is the detailed content of How to determine whether a php array is greater than 80. 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