0

I want to sort an array highest to lowest by the value of amount. My array $res is as follows:

Array
(
    [0] => 1
    [id] => 1
    [1] => Testowy 1
    [user] => Testowy 1
    [2] => 150
    [amount] => 150
    [3] => 1,2,3
    [what] => 1,2,3
    [4] => Polska
    [country] => Polska
    [5] => 1
    [platform] => 1
)
Array
(
    [0] => 2
    [id] => 2
    [1] => Testowy 2
    [user] => Testowy 2
    [2] => 100
    [amount] => 100
    [3] => 1
    [what] => 1
    [4] => United States
    [country] => United States
    [5] => 2
    [platform] => 2
)

I tried using max and arsort, but none of those seem to accept which key they should use for sorting. Any help?

3

4 Answers 4

1
usort($res, function ($a, $b){
    return $b['amount'] - $a['amount'];
});
print_r($res);

For versions of PHP < 5.3, use the following:

function cmp($a, $b){
    return $b['amount'] - $a['amount'];
}
usort($res, "cmp");
4
  • I tried usort, but I'm getting usort() expects parameter 1 to be array, boolean given. $res is from while($res = mysql_fetch_array($query)). Commented Aug 14, 2013 at 20:55
  • Isn't mysql_fetch_array returning array? Commented Aug 14, 2013 at 21:00
  • @TomekBuszewski - It should be, but you wouldn't be getting an error like that unless you were trying to feed usort a boolean value. Why don't you try dumping $res to see what you get.
    – Expedito
    Commented Aug 14, 2013 at 21:02
  • I used print_r($res) and I pasted it in my question. Commented Aug 14, 2013 at 21:03
1

Try usort

function cmp($a, $b)
{
    return ($a["amount"]<=$b["amount"])?-1:1;
}

usort($array, "cmp");
2
  • Maybe dont use strcmp, but a simple '>' cause amount is no string at all. Maybe wrong for sorting '1 and 10'.
    – dognose
    Commented Aug 14, 2013 at 19:38
  • Now you are missing the equal case :) return ($a["amount"]>=$b["amount"]) (>= or <= depending on the sorting order) will be enough.
    – dognose
    Commented Aug 14, 2013 at 19:43
0

Use sorting functions with user defined comparators, like: usort:

http://php.net/usort

Then your comparator gets two objects and tells (by any logic you want) which one is greater):

function compare($a, $b) {
    $result = -1;
    if( $a["amount"] == $b["amount"]) {
      $result = 0;
    } else {
       if( $a["amount"] > $b["amount"] ) {
          $result = 1;
       }
    }

    return $result;
}

usort($res, "compare");
3
  • is the 1 in $a1->($a1, $b) a typo?
    – Sean
    Commented Aug 14, 2013 at 19:35
  • Overkill. Simplest function Body: return ($a["amount"]>=$b["amount"]) (>= or <= depending on the sorting order) (or the substraction Version of Pe de Leao)
    – dognose
    Commented Aug 14, 2013 at 19:41
  • @dognose: nope. it's answer for newbie for him to understand. No place for tricks Commented Aug 14, 2013 at 22:09
0

Here is my solution, I know it is very late please don't mind

$data = [
    [
        'id' => 1,
        'user' => 'Testowy 1',
        'amount' => 100,
        'what' => '1,2,3',
        'country' => 'Polska',
        'platform' => 1,
    ],
    [
        'id' => 2,
        'user' => 'Testowy 2',
        'amount' => 130,
        'what' => '1',
        'country' => 'United States',
        'platform' => 2,
    ],
    [
        'id' => 3,
        'user' => 'Testowy 3',
        'amount' => 180,
        'what' => '1',
        'country' => 'United States',
        'platform' => 2,
    ],
];

usort($data, function ($a, $b) {
    $aValue = $a['amount'];
    $bValue = $b['amount'];

    // Sort in descending order
    return $bValue <=> $aValue;
});

echo "<pre>";
print_r($data);

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