search
HomeBackend DevelopmentPHP ProblemHow to find the minimum value in an array in php

3 methods: 1. Use "min($arr)" to obtain. 2. Use "sort($arr)" to sort in ascending order. After sorting, the first element is the minimum value, and use "reset($arr)" to obtain it. 3. Use "rsort($arr)" to sort in descending order, and use "end($arr)" to get the last element.

How to find the minimum value in an array in php

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

php seeks the smallest array Value method

In PHP, if you want to get the minimum value of an array, you can directly use the built-in function min();

You can also sort the array elements, If it is sorted in descending order, get the last element, if it is sorted in ascending order, get the first element.

Let’s take a closer look

1. Use the min() function

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

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

How to find the minimum value in an array in php

2. Sort in ascending order and get the first element

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

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

How to find the minimum value in an array in php

After sorting, the first element of the array is the minimum value, which can be taken out using the reset() function.

echo "数组最小值为: ".reset($arr);

How to find the minimum value in an array in php

3. Sort in descending order and get the last element

Use rsort to sort the array in descending order

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$arr=array(52,1,45,9,0,21,2,40,42);
rsort($arr);
var_dump($arr);
?>

How to find the minimum value in an array in php

After sorting, the last element is the minimum value, which can be obtained using the end() function.

echo "数组最小值为: ".end($arr);

How to find the minimum value in an array in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to find the minimum value in an array in php. 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 Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.