Steps: 1. Customize a function to process arrays and return elements that fail. The syntax is "function f($n){return($n
The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer
In an array, store some representative results How to find the number of failing grades in the array?
In PHP, you can use the array_filter() function and count() function to find the number of failed elements in the array.
Implementation steps:
Step 1: Customize a function and set specified conditions to process the array
The score is greater than or equal to 60 is a passing grade, and anything less than 60 is a failing grade. Therefore, the condition of the custom function is less than 60:
function f($num){ return($num<60); }
Step 2: Use array_filter() to call the custom function to process the specified array and return the elements that failed the grade
The array_filter() function uses a callback function to filter elements in an 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.
$res=array_filter($arr,"f");
array_filter() function returns the filtered array.
Step 3: Use the count() function to count the number of elements in the filtered array
$len=count($res)
The number of elements in the filtered array means a failing grade (less than 60) The number of elements.
Complete sample code:
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to find the number of failing grades in an array in php. For more information, please follow other related articles on the PHP Chinese website!