2

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.

2
  • 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. Commented Oct 8, 2013 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. Commented Oct 8, 2013 at 17:26

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I appreciate your help even though I am doing this wrong. Thank you!
0

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

2 Comments

$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.
If those are your limitations, then you must do what you have to do ;)
0

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

2 Comments

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.
I see...you can try this one, then: function cmp($a, $b) { return $a["price"] - $b["price"]; } usort($arr, "cmp");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.