[英]PHP - Find max/highest value
I have this query: 我有这个查询:
$stmt=$dbh->prepare("select round((expire - unix_timestamp()) / 86400) as days, count(*) as cnt from xeon_users_rented WHERE user_by=:username group by days;");
$stmt->bindParam(":username",$userdata['username']);
$stmt->execute();
$data=$stmt->fetchAll();
Which returns this array result var_dump($data)
: 这将返回此数组结果
var_dump($data)
:
array(2) {
[0]=>
array(4) {
["days"]=>
string(2) "27"
[0]=>
string(2) "27"
["cnt"]=>
string(1) "2"
[1]=>
string(1) "2"
}
[1]=>
array(4) {
["days"]=>
string(3) "116"
[0]=>
string(3) "116"
["cnt"]=>
string(1) "8"
[1]=>
string(1) "8"
}
}
I then want to get the highest value of the $data
result: 然后,我想获得
$data
结果的最高值:
echo max($data["days"]);
Althoug that doesn't work, as nothing is getting echoed out. Althoug不起作用,因为没有任何回声。 What am I doing wrong?
我究竟做错了什么?
wouldn't 不会
SELECT MAX(column_name) FROM table_name;
Do that in query already? 在查询中已经做到了吗? Selecting only the highest value - therefor saving bandwidth and wont require loops to waste even more resources.
仅选择最高值-从而节省带宽,并且不需要循环就浪费更多的资源。 All tho i cannot test it with your specific query.
我无法用您的特定查询来测试它。
$data['days']
isn't an array. $data['days']
不是数组。 You have an array of arrays, each of which has its own 'days' element. 您有一个数组数组,每个数组都有自己的'days'元素。
You could map your 'days' elements to an array and then get the max()
: 您可以将“ days”元素映射到数组,然后获取
max()
:
echo max(array_map(function($d){
return $d['days'];
}, $data));
As i see your array is 2 dimensional, you need to loop through both of this array and then get max from them. 如我所见,您的数组是二维的,您需要遍历这两个数组,然后从中获取最大值。
for($i=0;$i<=sizeof($data);$i++) { echo max($data[$i]["days"]); }
This should solve your problems. 这应该可以解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.