php - PHP中的Json对象排序
问题描述
我不专业,我有问题。我调用在数据库中使用 json 对象创建的助手。此代码按创建顺序调用,但我想将数字从大到小排序。我尝试了很多东西,但做不到。我需要你的帮助。谢谢
// add action to check for table item
add_action('gdlr_print_item_selector', 'gdlr_league_table', 10, 2);
function gdlr_league_table($type, $settings = array()){
if($type == 'gol-krali'){
gdlr_print_league($settings);
}
}
//table item
function gdlr_print_league($settings){
// query league table
$args['post_type'] = 'player';
$args['posts_per_page'] = (empty($settings['num-fetch']))? '555': $settings['num-fetch'];
$query = new WP_Query( $args );
// getting table array
while($query->have_posts()){ $query->the_post();
$player_val = gdlr_lms_decode_preventslashes(get_post_meta(get_the_ID(), 'gdlr-soccer-player-settings', true));
$assists = empty($player_val)? array(): json_decode($player_val, true);
$table[get_the_title()]['p'] = ($assists);
}
echo '<div class="gdlr-item gdlr-league-table-item" ' . $item_id . $margin_style . ' >';
if(empty($settings['style']) || $settings['style'] == 'full'){
gdlr_print_league_table($table);
}
echo '</div>';
}
// table
function gdlr_print_league_table($player){
echo '<table class="gdlr-league-table" >';
?>
<tr class="gdlr-table-second-head gdlr-title-font">
<th class="gdlr-table-pos"><?php echo __('Sıra', 'gdlr-soccer'); ?></th>
<th class="gdlr-table-team"><?php echo __('Oyuncu', 'gdlr-soccer'); ?></th>
<th class="gdlr-table-p">Asist</th>
</tr>
<?php
$count = 1;
foreach($player as $player_name => $score ){
?>
<tr>
<td class="gdlr-table-pos"><?php echo $count; ?></td>
<td class="gdlr-table-team"><?php echo $player_name ?></td>
<td class="gdlr-table-p"><?php echo $score['p']['player-stats']['assists']; ?></td>
</tr>
<?php
$count++;
}
echo '</table>';
}
解决方案
您可以在循环之前对$payer
数组进行排序:uasort
uasort($player, function($a, $b) {
return $b['p']['player-stats']['assists']-$a['p']['player-stats']['assists'];
});
foreach($player as $player_name => $score ){
推荐阅读
- python - TypeError:'numpy.int64'对象不可调用,每当我在python中调用len函数时?
- c++ - 2d char Array 打印方块。我如何解决它?
- swiftui - 在 SwiftUI 中更改 SpriteKit 场景
- python - 将值字符串传递到 Python 的 mySQL 连接器时遇到问题
- matlab - 如何避免 Matlab 中的积分器走出有效解决方案的领域?
- reactjs - 我可以将自定义匹配器与 toHaveBeenCalledWith 一起使用吗?
- c++ - 如果包含的类具有另一个类的成员,有没有办法在另一个类中包含一个类头?
- html - 旋转体的背景图片
- r - R 上的交互项 - 分类和连续曝光
- python - 拆分数据框并组合成一列python