关于小数的PHP min()函数

时间:2014-02-10 22:43:22

标签: php arrays decimal min

$con=mysqli_connect("localhost","root","password","eurusd");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$highs = array();
$lows = array();
$timestore = array();
$time = '23:00'; // start
for ($i = 0; $i <= 120; $i++)
{
    $prev = date('H:i', strtotime($time)); 
    $next = strtotime('+1mins', strtotime($time)); 
    $time = date('H:i', $next); // format the next time
    array_push($timestore, $prev);
    $result = mysqli_query($con,"
    SELECT * FROM eurusd2009 where date='"."2009-01-06"."'and time='".$timestore[$i]."'
    ");
    $row = mysqli_fetch_array($result);
    array_push($highs, $row[3]); //push highs to array
    array_push($lows, $row[4]); //push lows to array
}
print_r($timestore);
echo "<hr>";
print_r($highs);
echo "<hr>";
print_r($lows);
echo "<hr>";
$maxvaluehighs = max($highs);
$maxvaluelows = max($lows);

$minvaluehighs = min($highs);
$minvaluelows = min($lows);

echo $maxvaluehighs; //works
echo $maxvaluelows; //works

echo $minvaluehighs; //does not work
echo $minvaluelows; //does not work

数组示例

$highs = Array ( [0] => 1.353800 [1] => 1.353300 [2] => 1.352700 [3] => 1.353100 [4] => 1.352900 [5] => 1.352600 [6] => 1.352500 [7] => 1.352500 [8] => 1.352600 [9] => 1.353300 [10] => 1.352700 [11] => 1.352800 [12] => 1.352900 [13] => 1.353000 [14] => [15] => 1.353200 [16] => 1.353600 [17] => 1.353900 [18] => 1.353600 [19] => 1.353700 [20] => 1.353600 [21] => 1.353600 [22] => 1.353200 [23] => 1.353200 [24] => 1.353400 [25] => 1.353200 [26] => 1.353100 [27] => 1.353400 [28] => 1.353300 [29] => 1.353300 [30] => 1.353400 [31] => 1.353400 [32] => 1.353400 [33] => 1.353600 [34] => 1.353800 [35] => 1.353800 [36] => 1.353700 [37] => 1.353700 [38] => 1.353700 [39] => 1.353700 [40] => 1.353500 [41] => 1.353000 [42] => 1.353100 [43] => 1.353000 [44] => 1.352600 [45] => 1.352500 [46] => 1.352400 [47] => 1.352400 [48] => 1.352300 [49] => 1.352100 [50] => 1.352200 [51] => 1.352100 [52] => 1.352100 [53] => 1.352500 [54] => 1.352600 [55] => 1.352600 [56] => 1.351800 [57] => 1.352000 [58] => 1.352000 [59] => 1.351900 [60] => 1.355600 [61] => 1.354800 [62] => 1.355400 [63] => 1.355300 [64] => 1.354700 [65] => 1.354700 [66] => 1.354800 [67] => 1.354800 [68] => 1.354900 [69] => 1.355000 [70] => 1.354900 [71] => 1.354900 [72] => 1.355400 [73] => 1.355500 [74] => 1.355600 [75] => 1.355600 [76] => 1.354500 [77] => 1.353900 [78] => 1.353700 [79] => 1.352100 [80] => 1.351400 [81] => 1.351000 [82] => 1.351300 [83] => 1.351400 [84] => 1.351500 [85] => 1.351400 [86] => 1.352600 [87] => 1.352600 [88] => 1.352600 [89] => 1.352700 [90] => 1.352400 [91] => 1.352200 [92] => 1.351700 [93] => 1.351300 [94] => 1.351200 [95] => 1.351400 [96] => 1.351200 [97] => 1.351000 [98] => 1.351900 [99] => 1.352000 [100] => 1.352200 [101] => 1.352000 [102] => 1.351300 [103] => 1.351200 [104] => 1.351700 [105] => 1.351600 [106] => 1.351900 [107] => 1.352100 [108] => 1.351900 [109] => 1.352100 [110] => 1.352600 [111] => 1.352700 [112] => 1.352800 [113] => 1.352400 [114] => 1.352500 [115] => 1.352400 [116] => 1.351800 [117] => 1.351800 [118] => 1.352100 [119] => 1.352000 [120] => 1.351700 )

您好,我是初学者。我基本上试图使用PHP从数组中获取分钟和最大值。现在只有最大似乎工作,但最小不是。我尝试在线搜索解决方案,但我找不到一个解决方案,而且我很想知道下一步该尝试什么。我尝试使用:

 echo min(array_map('floatval',$highs));

但是没有运气。感谢。

2 个答案:

答案 0 :(得分:1)

$highs = Array (1.353800, 1.353300, 1.352700, NULL);

// if you want to leave only scalar values
$highs = array_filter($highs, 'is_scalar');

usort($highs, function($a, $b) {
    return bccomp($a, $b);
});

$min = reset($highs);

阅读更多如何比较float:http://www.php.net/manual/en/language.types.float.php

答案 1 :(得分:0)

如果数组中传递给min函数的任何值为NULL,则min将返回null。通过添加columnname is not null来处理查询中的这种情况

min(1.2, 2, null, -1)  // yields NULL not -1 

所以在你的脚本中,这样做是为了传递空值

if (!empty($row[4])) {
   array_push($lows, $row[4]); //push lows to array
}