-1

How can I sort this array by the value of the "num_of_offers" key? Currently i am getting array like this:

Array ( [0] => stdClass Object ( 
                        [ID] => 1 
                        [post_title] => abc 
                        [num_of_offers] => 0 
                        [offer_amt] => 12 
                       ) 
        [1] => stdClass Object ( 
                        [ID] => 2 
                        [post_title] => xyz 
                        [num_of_offers] => 1 
                        [offer_amt] => 12 
                       ) 
        [2] => stdClass Object ( 
                        [ID] => 3 
                        [post_title] => wxy 
                        [num_of_offers] => 2 
                        [offer_amt] => 17 
                       ) 
         [3] => stdClass Object ( 
                         [ID] => 4 
                         [post_title] => wxy 
                         [num_of_offers] => 2 
                         [offer_amt] => 44 
                        ) 
          )

But i want array like this:

Array ( [0] => stdClass Object ( 
                  [ID] => 5 
                  [post_title] => wxy 
                  [num_of_offers] => 2 
                  [offer_amt] => 44 
                 ) 
         [1] => stdClass Object ( 
                  [ID] => 3
                  [post_title] => wxy 
                  [num_of_offers] => 2 
                  [offer_amt] => 17 
                 ) 
         [2] => stdClass Object ( 
                  [ID] => 2 
                  [post_title] => xyz 
                  [num_of_offers] => 1 
                  [offer_amt] => 12 
                 ) 
          [3] => stdClass Object ( 
                  [ID] => 1 
                  [post_title] => abc 
                  [num_of_offers] => 0 
                  [offer_amt] => 12 
                 ) 
    )
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
user1990
  • 1,007
  • 1
  • 10
  • 17
  • Try using something like the [uasort function](http://php.net/manual/en/function.uasort.php). – Desaroll Dec 22 '15 at 18:13
  • 1
    If the question is vaguely readable, it only then becomes vaguely answerable. In future please spend a minute formatting your question so we dont have to scroll off to Jupiter to see all the relevant info – RiggsFolly Dec 22 '15 at 18:15
  • Possible duplicate of [How can I sort arrays and data in PHP?](http://stackoverflow.com/questions/17364127/how-can-i-sort-arrays-and-data-in-php) – Don't Panic Dec 22 '15 at 19:46

5 Answers5

4

Use either uasort() or usort()

usort($array, function($a, $b){
    if ($a->num_of_offers > $b->num_of_offers)
    {
        return -1;
    }
    else if ($a->num_of_offers < $b->num_of_offers)
    {
        return 1;
    }
    else
    {
        return 0;
    }
 });

It is actually unlikely that you'd want to use uasort in this case, because that would maintain the indexes in the new sorted array, and I don't think you want that. uasort() is useful if your indexes are meaningful strings or something along those lines.

php.net usort()
http://php.net/manual/en/function.usort.php

php.net uasort()
http://php.net/manual/en/function.uasort.php

Chris Trudeau
  • 1,427
  • 3
  • 16
  • 20
0

If your array is the result of a database query, let the database do the sorting (by appending ORDER BY num_of_offers to the query).

ax.
  • 58,560
  • 8
  • 81
  • 72
0

Try this way, not tested so make change if you need

function cmp($a, $b)
{
    return ($a->num_of_offers < $b->num_of_offers);
}

usort($your_data, "cmp");
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
0

Using a predefined callback function to sort you could try ( not tested ):

function snorted( $a, $b ){
    return $a->num_of_offers===$b->num_of_offers ? 0 : ( ( $a->num_of_offers < $b->num_of_offers ) ? -1 : 1 );
}
usort( $arr, 'snorted' );
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
0

Try a usort. If you are still on PHP 5.2 or earlier, you'll have to define a sorting function first:

function sortByNumOfOffers($a, $b) {
    return $a['num_of_offers'] - $b['num_of_offers'];
}

usort($myArray, 'sortByOrder');

Starting in PHP 5.3, you can use an anonymous function:

usort($myArray, function($a, $b) {
    return $a['num_of_offers'] - $b['num_of_offers'];
});

And finally with PHP 7 you can use the spaceship operator:

usort($myArray, function($a, $b) {
    return $a['num_of_offers'] <=> $b['num_of_offers'];
});

To extend this to multi-dimensional sorting, reference the second/third sorting elements if the first is zero - best explained below. You can also use this for sorting on sub-elements.

usort($myArray, function($a, $b) {
    $retval = $a['num_of_offers'] <=> $b['num_of_offers'];
    if ($retval == 0) {
        $retval = $a['sub_num_of_offers'] <=> $b['sub_num_of_offers'];
        if ($retval == 0) {
            $retval = $a['details']['sub_sub_num_of_offers'] <=> $b['details']['sub_sub_num_of_offers'];
        }
    }
    return $retval;
});

If you need to retain key associations, use uasort() to compare array sorting functions in the manual.

Alaa M. Jaddou
  • 1,180
  • 1
  • 11
  • 30