views:

160

answers:

2

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] => 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

A: 

use array_multisort

wnoveno
please elaborate answer and give examples.
thephpdeveloper
http://us2.php.net/manual/en/function.array-multisort.php
Mark
@Mark i know. wnoveno should at least provide a better answer.
thephpdeveloper
+1  A: 

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');
thephpdeveloper
Thanks Mauris - doesn't seem to have worked. I wish it wasn't this hard. Here's the output using this:-$length = count($alisting);for ($i = 0; $i < $length; $i++) { echo $alisting[$i]['questions'].'<br>';}23153434318
Jonathan Lyon
hi Jonathan, i noticed that your key is 'questions', not 'Question'. you might want to check that...
thephpdeveloper
yep - I changed that function sort_callback($a, $b){ if ($a['questions'] == $b['questions']) { return 0; } return ($a['questions'] < $b['questions']) ? 1 : -1;}
Jonathan Lyon
Hi there i noticed it's because you are using $i for the index. the index are not changed, the order are. you might want to use var_dump to see it, or use a foreach.
thephpdeveloper
This is driving me mad - why is it so hard - can someone please help with a code example that can work - I really need this
Jonathan Lyon
excellent the var_dump shows that the sort has worked - thank you so much - how do I loop through the array and echo it out to a list and preserve the sort order?
Jonathan Lyon
it seems that the vardump shows that the order is correct but it lists all the question rows in descending order then all the title fields and then the ID's - I really need to output all elements together but in descending order of the questions element - any ideas anyone?
Jonathan Lyon
Hi there sorry I was out for the day. You will need to use a `foreach` instead of `for` in order to loop through the array in the correct order.
thephpdeveloper