Home > database >  The max value within a foreach loop php
The max value within a foreach loop php

Time:04-14

Hope you are doing well, I am sorry if my question is a bit silly, but I have spent hours trying to find a solution to my problem but without any success, can somebody help me?

I am trying to find the max value within my foreach loop, and when I try to use the max() function, it doesn't show any result at all ...

foreach ($decode2 as $value) {
    $maak = max($value->price_change_percentage_24h);
}

echo $maak;

and when I do the simple test with :

foreach ($decode2 as $value) {    
    echo $value->price_change_percentage_24h;   
}

I can see all my data ...

Please any help would be very appreciated ! :)

CodePudding user response:

You need to pass several arguments to the max function, so that it returns the maximum value.

You should probably do something like this:

<?php 

foreach ($decode2 as $value) {
    $maak = max($maak, $value->price_change_percentage_24h);   
}

echo $maak;

?>

Have a look here if you have any doubts: https://www.php.net/manual/fr/function.max.php

This includes some examples as well

  • Related