dsbx40787736 2014-04-27 22:15
浏览 56

根据变量对foreach循环进行排序

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:

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

  • 写回答

1条回答 默认 最新

  • dqstti8945 2014-05-17 14:46
    关注

    ACF is an awesome plugin. I use it for everything.

    I see you are using sub_fields so I assume you are using a repeater.

    ACF uses this format array_multisort( $column_id, SORT_ASC, $repeater );

    This page may help you out a lot, shows how to sort fields in ACF.

    http://www.advancedcustomfields.com/resources/how-to/how-to-sorting-a-repeater-field/

    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂