Reputation: 15
I had problem with array_push
so I needed to make numeric array instead of associative. But, as I was doing it, I always pushed 2 values:
So if we would have, let's say, 4
objects with ID's: 1,2,3,4
and we would have given them some values: 20, 50, 40, 30
(going from the lowest ID), my array looks like:
1, 20, 2, 50, 3, 40, 4, 30.
How can I sort my array from the highest value? The result should be array which looks like:
2, 50, 3, 40, 4, 30, 1, 20.
Upvotes: 0
Views: 145
Reputation: 131891
PHP support nested (multi-dimensional) arrays
$array = [
[1, 20],
[2, 50],
[3, 40],
[4, 30]
];
Now you can easly sort it
usort(
$array,
function ($left, $right) { return $right[1] - $left[1]; }
);
var_dump($array);
Depending on what this ID and value is, you may consider creating a class instead, especially when there exists other values associated with this ID.
Update: Oh, and because you mentioned
first was the key to search (ID of object in table) and second was some value that was given to object with ID.
When you are talking about database tables, let the database sort for you ;)
ORDER BY `valueColumn` DESC
Upvotes: 1