Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

The max value within a foreach loop php

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

I can see all my data …

Please any help would be very appreciated ! 🙂

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading