Question

$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

array example

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

Hello, I am a beginner at this. I'm basically trying to get the mins and maxs from an array using php. For now only the max seems to be working, but min isn't. I tried searching for a solution online but I couldn't find one, and I have very little idea what to try next. I tried using:

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

but have had no luck with it. Thanks.

Was it helpful?

Solution 2

If any of your values in the array that is passed to min function is NULL, min will return null. Take care of that situation in your query by adding columnname is not null

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

so in your script, do this to pass the null values

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

OTHER TIPS

$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);

Read more how to compare float: http://www.php.net/manual/en/language.types.float.php

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top