Home > Backend Development > PHP Problem > How to find the number of elements greater than 0 in an array in php

How to find the number of elements greater than 0 in an array in php

青灯夜游
Release: 2023-03-16 18:18:02
Original
2855 people have browsed it

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)".

How to find the number of elements greater than 0 in an array in php

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(&#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 find the number of elements greater than 0 in an array in php

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(&#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);

$len=count($res);
echo "数组中大于0的元素个数为:".$len;
?>
Copy after login

How to find the number of elements greater than 0 in an array in php

Description:

1, array_filter

array_filter — Use the callback function to filter the elements of the array

array_filter(array $array, ?callable $callback = null, int $mode = 0): array
Copy after login

Parameters:

◇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:

  • ARRAY_FILTER_USE_KEY - Use the key name as the only parameter of the callback callback, not the value

  • ARRAY_FILTER_USE_BOTH - Pass both the value and the key as arguments to the callback instead of just passing the value

  • The default value is 0, passing only the value as the callback The only parameter.

Return value: Returns the filtered array.

  • array_filter iterates through each value in the array array and passes each value to the callback callback function. If the callback function returns true, the current value in array is returned to the result array.

  • The key names (subscripts) of the returned result array array will remain unchanged. If the array parameter is an index array, the returned result array key names (subscripts) may be discontinuous. . Arrays can be reindexed using the array_values() function.

  • When the array_filter() function is used to declare a callback function, it will delete false values ​​(null values), however, if the callback function is not specified, the value in the array equal to FALSE will be deleted All elements, such as empty strings or NULL values.

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 )
Copy after login

Parameter description is as follows:

    $array: is the array or object to be counted;
  • $mode: is an optional parameter , can be omitted.
    • If the $mode parameter is omitted, or set to COUNT_NORMAL or 0, the count() function will not detect multidimensional arrays;
    • If $mode is set to COUNT_RECURSIVE or 1, the count() function Will recursively count the number of elements in the array, especially useful for counting the number of elements in multi-dimensional arrays.
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!

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