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

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

青灯夜游
青灯夜游Original
2022-07-20 20:10:352998browse

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);
?>

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的";
}
?>

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!

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