search
HomeBackend DevelopmentPHP ProblemHow to remove the maximum and minimum values ​​from a php array and then calculate the average

Method: 1. Sort the array in ascending order, and use "array_pop(array)" and "array_shift(array)" to remove the maximum and minimum values; 2. Use "count(array)" and "array_sum( Array)" to obtain the array length and element sum; 3. Use "element sum/array length" to calculate the average.

How to remove the maximum and minimum values ​​from a php array and then calculate the average

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Remove the largest php array Method of averaging after values ​​and minimum values

1. Remove the maximum and minimum values ​​from the array

  • Use sort () Sort the array in ascending order

  • After sorting, the first element of the array is the minimum value, and the last element is the maximum value. Just use the array_pop() and array_shift() functions Just delete these two values.

    • array_pop(): Delete the last element in the array.

    • array_shift(): Delete the first element in the array.

<?php
header("Content-type:text/html;charset=utf-8");
$arr=array(1,45,9,52,0,-5,21,-1,40);
sort($arr);
var_dump($arr);
array_pop($arr);        //去掉最大值
array_shift($arr);       //去掉最小值
var_dump($arr);
?>

How to remove the maximum and minimum values ​​from a php array and then calculate the average

2. Statistics of the array length and the sum of array elements after removing the maximum and minimum values

Because the array_pop() and array_shift() functions will modify the original array, you can directly use the count() function to obtain the processed original array length.

$len=count($arr);
echo "处理后的数组长度为:".$len;

How to remove the maximum and minimum values ​​from a php array and then calculate the average

Use array_sum() directly to get the sum of array elements:

$sum=array_sum($arr);
echo "<br>处理后的数组元素之和为:".$sum;

How to remove the maximum and minimum values ​​from a php array and then calculate the average

3. Find the average: Use the "sum of array elements" divided by the "array length"

$num=$sum/$len;
echo "<br>平均数为:".$num;

How to remove the maximum and minimum values ​​from a php array and then calculate the average

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to remove the maximum and minimum values ​​from a php array and then calculate the average. 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools