dqyq88053 2017-01-05 19:07
浏览 44
已采纳

查找深层嵌套数组中特定键的最低值

Given an array with the following structure:

$orders = [
     0 => [
              'volume' => 1926,
              'lowPrice' => 1000.00,
              'date' => 2016-12-29 00:45:23
     ],
     1 => [
              'volume' => 145,
              'lowPrice' => 1009.99,
              'date' => 2016-12-31 14:23:51
     ],
     2 => [
              'volume' => 19,
              'lowPrice' => 985.99,
              'date' => 2016-12-21 09:37:13
     ],
     3 => [
              'volume' => 2984,
              'lowPrice' => 749.99,
              'date' => 2017-01-01 19:37:22
     ],
     // ...
]

I'm looking for the most performant way to find the lowest value for lowPrice. The size of $orders will more than likely be larger than 500 items. For the given array, the result would be as follows:

$lowestPrice = $this->findLowestPrice($orders);

echo $lowestPrice; // 749.99
  • 写回答

2条回答 默认 最新

  • doubiankang2845 2017-01-05 19:28
    关注

    Extract an array of lowPrice and find the min():

    echo min(array_column($orders, 'lowPrice'));
    

    Since those appear to be floats, you'll probably want number_format() or money_format().

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?