dpka7974 2014-11-04 14:40
浏览 220
已采纳

PHP - 查找最大/最高值

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):

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:

echo max($data["days"]);    

Althoug that doesn't work, as nothing is getting echoed out. What am I doing wrong?

  • 写回答

3条回答 默认 最新

  • dongxing9219 2014-11-04 14:42
    关注

    $data['days'] isn't an array. You have an array of arrays, each of which has its own 'days' element.

    You could map your 'days' elements to an array and then get the max():

    echo max(array_map(function($d){
        return $d['days'];
    }, $data));
    

    Demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型