开发者

Sort php array on one column and output to list

I have created an array that is populated from a wordpress loop:-

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

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

<?php endwhile; ?>

This outputs

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] =&g开发者_如何转开发t; Array ( [questions] => 34 ) [10] => Array ( [id] => 295 )

I need to sort this array by Questions in descending order and echo it out to a list so that I will have:-

ID       | Title           |  Question

1023       Cash Commons       43
987        Videowtf           34

etc

Can someone please help with very clear instructions please.

Thanks in advance of your help.

Cheers

Jonathan


You will need to use the function uasort() - http://www.php.net/manual/en/function.uasort.php - and create a call back function to sort.

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

uasort($alisting,'sort_callback');


use array_multisort

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜