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 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历