search
HomeBackend DevelopmentPHP ProblemHow to find the maximum and minimum value of a php array

Two methods: 1. Use the "max(array)" statement to obtain the maximum value, and use the "min(array)" statement to obtain the minimum value. 2. Use "sort(array)" to sort the array in ascending order, use "array_key_first(array)" to get the minimum value, and "array_key_last(array)" to get the maximum value.

How to find the maximum and minimum value of a php array

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

In php, there are many ways to obtain For the maximum and minimum values ​​of the array, this article will introduce two methods to you:

Method 1: Use the max() and min() functions

  • max() function returns the maximum value in an array

  • min() function returns the minimum value in an array

Example:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$arr=array(1,2,3,4,5,6,7,8,9);
echo "数组最大值为: ".max($arr)."<br>";
echo "数组最小值为: ".min($arr)."<br>";
?>

How to find the maximum and minimum value of a php array

Method 2: Use sort(), array_key_first(), array_key_last() functions

You can use sort() to sort the array in ascending order

<?php
$arr=array(52,1,45,9,0,21,-1,40,-5);
sort($arr);
var_dump($arr);
?>

How to find the maximum and minimum value of a php array

It can be seen that the first element of the array is the minimum value, and the last element is the maximum value. Just take out these two values.

So how to get the first element and the last element of the array? You can use the array_key_first() and array_key_last() functions

  • ##array_key_first() to get the first element of the specified array. A key value.

  • array_key_last() Gets the last key value of the specified array.

  • echo "数组最大值为: ".array_key_last($arr)."<br>";
    echo "数组最小值为: ".array_key_first($arr)."<br>";

How to find the maximum and minimum value of a php array

Note: PHP versions of these two functions: PHP 7 >= 7.3.0, PHP 8

recommended Study: "

PHP Video Tutorial"

The above is the detailed content of How to find the maximum and minimum value of a php array. 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

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool