0);}$res=array_filter($arr," f");", returns a filtered array; 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. If they are equal, the array elements are greater than 0, otherwise there are less than or Element equal to 0."/> 0);}$res=array_filter($arr," f");", returns a filtered array; 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. If they are equal, the array elements are greater than 0, otherwise there are less than or Element equal to 0.">
Home > Article > Backend Development > How to determine if all array elements are greater than 0 in php
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.
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('content-type:text/html;charset=utf-8'); 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); ?>
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('content-type:text/html;charset=utf-8'); 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的"; } ?>
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!