首页 > 解决方案 > 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>';
}

标签: phpjsonwordpressobject

解决方案


您可以在循环之前对$payer数组进行排序:uasort

uasort($player, function($a, $b) {
    return $b['p']['player-stats']['assists']-$a['p']['player-stats']['assists'];
});
foreach($player as $player_name => $score ){

推荐阅读