Home > Article > Backend Development > How to get the largest value in a set of data_PHP Tutorial
Question
How to get the data with the largest median value in a set
Solution
There is a set of data that is not called in order of ID.
I want to get the data with the largest ID. What do I do.
Generally, all ID values are put into an array and then compared in size.
But is there any other way? Because I am only trying to retrieve the maximum ID value for data manipulation this time. If I make it into an array and compare it, it will consume more resources.
[ ]
Reference answer
Just compare keys!
Reference answer
Original post by liexusong on 2009-1-4 00:09 [url=http://bbs.111cn.cn/redirect.php?goto=findpost&pid=814074&ptid=100300]Link tag [img]http://bbs. 111cn.cn/images/common/back.gif[/img][/url]
Just compare keys!
Wouldn't it be better to put it in the array?
Reference answer
$the_max = NULL;
foreach($array as $key => $value) {
if($key > $the_max) $the_max = $key;
}
echo $array[$the_max]; //This is the answer
Copy code
Reference answer
The original post was posted by liexusong on 2009-1-4 00:11 [url=http://bbs.111cn.cn/redirect.php?goto=findpost&pid=814077&ptid=100300]Link tag [img]http://bbs. 111cn.cn/images/common/back.gif[/img][/url]
$the_max = NULL;
foreach($array as $key => $value) {
if($key > $the_max) $the_max = $key;
}
echo $array[$the_max]; //This is the answer
Thank you.
Reference answer
I didn’t have FOREACH in the end, I just IF
This leaves only combining the data into arrays. [img]http://www.111cn.cn/bbs/images/smilies/default/lol.gif[/img]
Reference answer
Thank you for the ideas you provided.
Reference answer
Isn’t it possible to max??
Reference answer
$sql = 'SELECT * FROM `table` ORDER BY `id` DESC LIMIT 1';
Copy code