Home > Backend Development > PHP Problem > How to determine if all array elements are greater than 0 in php

How to determine if all array elements are greater than 0 in php

青灯夜游
Release: 2023-03-16 15:34:02
Original
3158 people have browsed it

Judgment steps: 1. Use the array_filter() function to call the callback function to filter the array and return elements greater than 0. The syntax "function f($num){return($num>0);}$res= array_filter($arr,"f");", returns a filtered array; 2. Use the count() function to obtain the lengths of the original array and the filtered array, and compare whether the lengths of the two arrays are equal. If they are equal, the array elements are greater than 0. Otherwise, there are elements less than or equal to 0.

How to determine if all array elements are greater than 0 in php

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

In php, you can use array_filter() and The count() function determines whether all array elements are greater than 0.

Implementation steps:

Step 1: Use the array_filter() function to filter the array and return elements greater than 0 in the array

array_filter() function uses the callback function to filter the elements in the array and returns a filtered array.

This function passes each key value in the input array to the callback function. If the callback function returns true, the current key value in the input array is returned to the result array. Array key names remain unchanged.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
function f($num){
	 return($num>0);
}
$arr=array(2,-1,4,-8,-10,-5,9);
var_dump($arr);
$res=array_filter($arr,"f");
echo "过滤不大于0的数组后:";
var_dump($res);
?>
Copy after login

How to determine if all array elements are greater than 0 in php

Step 2: Use the count() function to obtain the length of the original array and the filtered array, and compare whether the lengths of the two arrays are equal

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
function f($num){
	 return($num>0);
}
$arr=array(2,-1,4,-8,-10,-5,9);
var_dump($arr);
$res=array_filter($arr,"f");
$len1=count($arr);
$len2=count($res);
if($len1==$len2){
	echo "数组元素都大于0";
}else{
	echo "数组元素不都大于0,有小于或等于0的";
}
?>
Copy after login

How to determine if all array elements are greater than 0 in php

If the lengths of the original array and the filtered array are not equal, it means that the array_filter() function has processed the original array (elements that do not meet the conditions have been deleted), that is, there are elements in the original array that are less than Or elements equal to 0, so not all array elements are greater than 0.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to determine if all array elements are greater than 0 in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template