Get highest value from foreach loop PHP

Hi everyone!

My script below are looping out value of bids and date and works fine.
I want to make a function with this script to only show the latest value in the loop or the highest value.

Hope you can help me, I’m in a little bit hurry.
BIG THANKS IN ADVANCED!

<?php if ( !empty( $obj->Bids->Bid) ) {
    $counter = 0;
    $bids = is_array($obj->Bids->Bid) ? $obj->Bids->Bid : array($obj->Bids->Bid);
    ?>
    <div id="fasad-description-bidhistory">
        <h3><?= __('Bid history', 'fasad-starter');?></h3>
        <ul id="fasad-bidhistory-item-list" class="fasad-list">

            <?php foreach ( $bids as $index => $bid) : ?>
                <li>
                    <?php echo ($index+1) ?> <br>
                    <?= __('Bid', 'fasad-starter');?>: <?php echo number_format($bid->amount, 0, '', ' ')?> <?= __('SEK', 'fasad-starter');?> <br>
                    <?= __('Date', 'fasad-starter');?>: <?php echo $bid->date; ?>
                </li>
            <?php endforeach; ?>
        </ul>
    </div>
<?php } ?>

Where do you get the bids from? If you get them from a database, then alter the query to pull out only the largest bid. You would use the MySQL function MAX(fieldname) to locate it. Then, in your list of bids, just have the one largest one. If you load the bids some other way, you would have to run a foreach() loop and locate the largest one first, then display only that one. Loosely like this…

$largest_bid="";
foreach(bids as $index=>$bid) {
if ($bid>$largest_bid) {$largest = $index;}
} // end of loop
//Now largest_bid variable holds the index of the correct bid.
echo $bids[$largest_bid];

Something like that should work. As long as there are no duplicate bids in your $bids array…

Just sort the array by date. There should be a constraint to add the bid that it be larger than the last, correct?

Sponsor our Newsletter | Privacy Policy | Terms of Service