Home > Backend Development > PHP Problem > How to find the number of failing grades in an array in php

How to find the number of failing grades in an array in php

青灯夜游
Release: 2023-03-16 15:38:01
Original
2254 people have browsed it

Steps: 1. Customize a function to process arrays and return elements that fail. The syntax is "function f($n){return($n

How to find the number of failing grades in an array in php

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

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

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

The number of elements in the filtered array means a failing grade (less than 60) The number of elements.

Complete sample code:

Copy after login

How to find the number of failing grades in an array in php

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!

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