Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to find the maximum value in an array by php

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "how to find the maximum value in the array". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Two methods: 1, use max () to get the maximum value, syntax "max ($arr)". 2. Use "arsort ($arr)" to sort the array in descending order. The first element of the array after sorting is the maximum value, which can be taken out with "reset ($arr)" or "array_key_first ($arr)".

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

Two methods of finding the maximum value in Array by php

Method 1: directly use the built-in function max ()

The max () function can return the maximum value in an array

Method 2: sort the array elements in descending order and take out the first element

Use arsort () to sort the array in descending order

$arr=array (52 arr); var_dump ($arr)

Then the first element of the array, which is the maximum value needed, can be taken out.

To get the first element, you can use the reset () or array_key_first () function.

Array_key_first () gets the first key value of the specified array.

The reset () function points the inner pointer in the array to the first element and returns the value of the first array element, or the Boolean value False if the array is empty.

The maximum value of the echo array is: ".reset ($arr)."

The maximum value of the "; echo" array is: ".array _ key_first ($arr)

This is the end of the content of "how to find the maximum value in an array by php". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report