【问题标题】:Json Object Sorting in Php [closed]Php中的Json对象排序[关闭]
【发布时间】:2019-03-15 20:22:16
【问题描述】:

我不专业,我有问题。 我调用在数据库中使用 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>';
}

【问题讨论】:

    标签: php json wordpress object


    【解决方案1】:

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

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

    【讨论】:

    • 感谢您的回答。通常排序如下;ibb.co/hVh2nPH,当我使用这段代码时; ibb.co/vs4wH0m.
    • 我想将数字从高到低排序。
    • 啊,我明白了,$score 不是数字,而是嵌套数组。我更新了我的答案。
    • 啊它成功了。非常感谢。
    • 你好。我有一个新问题。其中一些球员没有这些号码,但他们打电话给他们。我不想给他们看。我可以只显示数字吗?
    猜你喜欢
    • 2013-12-16
    • 2020-11-12
    • 2016-01-26
    • 1970-01-01
    • 1970-01-01
    • 2020-06-18
    • 2018-01-24
    • 2015-07-16
    • 2023-03-03
    相关资源
    最近更新 更多