0);}$res=array_filter($arr," f");" will return a filtered array containing elements greater than 0; 2. Use the count() function to obtain the length of the filtered array, which is the number of elements greater than 0, using the syntax "count (filtered array)"."/> 0);}$res=array_filter($arr," f");" will return a filtered array containing elements greater than 0; 2. Use the count() function to obtain the length of the filtered array, which is the number of elements greater than 0, using the syntax "count (filtered array)".">
Home >Backend Development >PHP Problem >How to find the number of elements greater than 0 in an array in php
Implementation 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");" will return a filtered array containing elements greater than 0; 2. Use the count() function to obtain the length of the filtered array, which is the number of elements greater than 0. The syntax "count (filter array)".
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
In php, you can use array_filter () function and count() function to find the number of elements in an array greater than 0.
Implementation steps:
##Step 1: Use the array_filter() function to filter the array, Returns the elements in the array that are greater than 0
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); ?>After filtering the elements in the array that are not greater than 0, the elements contained in the returned filtered array are all greater than 0.
Step 2: Use the count() function to obtain the length of the filtered array, that is, the number of elements greater than 0
count() function returns the elements in the array Number of.<?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); $len=count($res); echo "数组中大于0的元素个数为:".$len; ?>
Description:
1, array_filter
array_filter — Use the callback function to filter the elements of the arrayarray_filter(array $array, ?callable $callback = null, int $mode = 0): arrayParameters: ◇array: the array to be traversed ◇callback: the callback function used If no callback function is provided, all "empty" elements of array in the array will be deleted. See empty() for how PHP determines "empty" elements. ◇Mode determines which parameters are sent to the callback flag:
2. count()
The count() function can count the number of all elements in the array, or the number of attributes in the object , its syntax format is as follows:count($array , $mode )Parameter description is as follows:
Tip: If $array is neither an array nor an object, the count() function will return 1; if $array is equal to NULL, the count() function Return 0.sizeof() function is an alias of count() function, that is, the function and usage of sizeof() function are exactly the same as count() function. Recommended learning: "
PHP Video Tutorial"
The above is the detailed content of How to find the number of elements greater than 0 in an array in php. For more information, please follow other related articles on the PHP Chinese website!