簡體   English   中英

在一列上排序php數組並輸出到列表

[英]Sort php array on one column and output to list

我創建了一個從wordpress循環填充的數組:-

<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>

        $alisting []["questions"] = $questions; 
        $alisting []["id"] = $post->ID; 
        $alisting []["title"] = $post->post_title; 

<?php endwhile; ?>

這個輸出

Array ( [0] => Array ( [questions] => 22 ) [1] => Array ( [id] => 1016 ) [2] => Array ( [title] => Cash Commons ) [3] => Array ( [questions] => 15 ) [4] => Array ( [id] => 811 ) [5] => Array ( [title] => The Poker Echo ) [6] => Array ( [questions] => 34 ) [7] => Array ( [id] => 437 ) [8] => Array ( [title] => VideoWTF.com ) [9] => Array ( [questions] => 34 ) [10] => Array ( [id] => 295 )

我需要按問題按降序對該數組排序,並將其回顯到列表中,這樣我將具有:

ID       | Title           |  Question

1023       Cash Commons       43
987        Videowtf           34

等等

有人可以提供非常清晰的指示嗎?

在此先感謝您的幫助。

干杯

喬納森

您將需要使用的功能uasort() - http://www.php.net/manual/en/function.uasort.php -並創建一個回調函數來進行排序。

function sort_callback($a, $b){
    if ($a['Question'] == $b['Question']) {
        return 0;
    }
    return ($a['Question'] < $b['Question']) ? 1 : -1;
}

uasort($alisting,'sort_callback');

使用array_multisort

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM