Sort a foreach loop based on variable

I have a frontend table form (using Advanced Custom Fields plugin for WordPress) for users to track the number of days each week that they ride their bike to work. Each person has their own row and enters their number for each week. Then I loop through to add up the total number entered for each row and list each person’s name along with their total.

How can I sort this so that the names are listed in order by the highest total ($overalltotal)? Here is my code, which correctly lists the names along with their totals, but it is unsorted:

Read More
 $rows = get_sub_field('ride_tracker');

 foreach( $rows as $row )
    {

    $overalltotal = intval($row['week_1'] + $row['week_2'] + $row['week_3'] + $row['week_4']);

    echo $row['name'] . ' : ' . $overalltotal . '<br />' ;

    }

I’ve read about array_multisort and usort, but I can’t figure out how to get these to work with a variable that’s calculated during the loop.

Thank you for your help!

Related posts

Leave a Reply

1 comment