Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Is it possible to take the results from an array from a database query and sort by one of the columns in the results? If so, how would that be done?

Here is an example of the array that I have:

Array ( [0] => stdClass Object ( [item] => 0 [size] => 2056515 [group] => SF2K [description] => 205/65R15 SAFFIRO SF2000 94V 40K [sort4] => 94 [sort5] => V [sort6] => [price] => 69.14 ) [1] => stdClass Object ( [item] => 8518 [size] => 2056515 [group] => FTOUR [description] => 205/65R15 FUZION TOUR BW 94H 40K [sort4] => 94 [sort5] => H [sort6] => [price] => 77.63 ) [2] => stdClass Object ( [item] => 106633 [size] => 2056515 [group] => SEREN [description] => 205/65R15 BS TURANZA SER+ 94H 80K [sort4] => 94 [sort5] => H [sort6] => [price] => 124.07 ) )

I would like to sort it by [price]. I know that in this example they are all already in numerical order, but they are not always.

share|improve this question
    
If you really want to do this in PHP, you can use the approach described here: stackoverflow.com/a/19253751/1615903 - however, ORDER BY statement in your SQL query will be much better. –  1615903 Oct 8 '13 at 17:21
    
I have the database ordered by price and I have an ORDER BY in my SQL query, the problem is another function that I cannot remove from the template changing the ORDER BY after my query. This is not a permanent solution, just a temporary fix so I have time for a real solution. I appreciate your help. –  Willee5586 Oct 8 '13 at 17:26

3 Answers 3

up vote 1 down vote accepted

Real solution is to fix your code so you can use order by statement in your query.

That said, to do this in php, create a comparator function:

function myComparator($a, $b) {
    return $a->price - $b->price;
}

Then, on your array, let's call it $myArray, call:

usort($myArray, 'myComparator');

See php.net documentation on usort for more details.

share|improve this answer
    
I appreciate your help even though I am doing this wrong. Thank you! –  Willee5586 Oct 8 '13 at 17:33

You can do this with the SQL query itself. Add one of the following to the end of your query:

Ascending order (lowest first):

ORDER BY price ASC

Descending order (highest first):

ORDER BY price DESC
share|improve this answer
    
$tire_sql = $wpdb->prepare( "SELECT item, size, group, description, sort4, sort5, sort6, price FROM _tires WHERE size = %d AND sort5 != '' ORDER BY price ASC", $size ); That is what I currently have, my problem is that this page template is heavy incorporated into the theme of the website. There is a function that is calling an order by itself after my query. So I need to resort. I know this is a horrible thing to do, but I am just in a crunch to get it working, then I will have time to rewrite it after. –  Willee5586 Oct 8 '13 at 17:19
    
If those are your limitations, then you must do what you have to do ;) –  SamT Oct 8 '13 at 21:55

You can just add "ORDER BY price DESC(or ASC, depending on your needs)" to your SQL query

share|improve this answer
    
After my sql query and before my results display there is a function that is part of the WordPress template that I cannot change or remove that is changing the order after my query. So I was hoping there was a way to sort the array by one of the columns from the db that I can add after the template's function that is reordering. –  Willee5586 Oct 8 '13 at 17:23
    
I see...you can try this one, then: function cmp($a, $b) { return $a["price"] - $b["price"]; } usort($arr, "cmp"); –  Lucone83 Oct 8 '13 at 17:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.