douzhanhui5662 2019-02-14 09:38
浏览 122
已采纳

获取PHP数组中的最低值和最高值不会向我显示正确的数字

I have the following array:

Array
(
    [0] => 124,95
    [1] => 139,95
    [2] => 149,95
    [3] => 1200
    [4] => 150
    [5] => 154,95
    [6] => 130
    [7] => 189,95
    [8] => 199,95
    [9] => 30
    [10] => 150
)

And I am trying to get the lowest (30) and highest (1200) numbers from it.

So I did this:

while($getpricesproducts = $getpricesproductscon->fetch_assoc()){
  $prijsarray[] = $getpricesproducts['prijs'];
}

// Lowest and highest price to put in price slider
$prijslow = min($prijsarray);
$prijshigh = max($prijsarray);

echo $prijslow;

echo $prijshigh;

$prijsarray is above array.

But the echoed values are 30 as min number and 150 as max. Why is that? Does it have something to do with the commas in some numbers? Still it is weird that 1200 is not the max number since it does not have any commas.

  • 写回答

3条回答 默认 最新

  • doufengsui7449 2019-02-14 10:05
    关注

    You can use array_map with strtr to convert all the numbers in your array from their current format to floating point. Then you can take the min and max and use number_format to convert them back to your format:

    $new_array = array_map(function ($v) { return (float)strtr($v, array(',' => '.', '.' => '')); }, $array);
    echo number_format(min($new_array), 2, ',', '.') . "
    ";
    echo number_format(max($new_array), 2, ',', '.') . "
    ";
    

    Output:

    30,00
    1.200,00
    

    Demo on 3v4l.org

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

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料