-1

I have an array with a bunch of keys. I want to sort one of the keys by their values.

Array ( 
   [0] => stdClass Object ( 
                            [id] => 1 
                            [question] => Action 
                            [specific_to_movie_id] => 1
                            [total_yes] => 4 ) 
   [1] => stdClass Object ( 
                            [id] => 2 
                            [question] => Created by DC Comics 
                            [specific_to_movie_id] => 1 
                            [total_yes] => 1 ) 
   [2] => stdClass Object ( 
                            [id] => 3 
                            [question] => Christian Bale 
                            [specific_to_movie_id] => 1 
                            [total_yes] => 1 ) 
   )

The array looks like that above, and I want to sort by "Total_yes"

How can I go about doing this in PHP?

One Man Crew
  • 9,420
  • 2
  • 42
  • 51
Daniel Fein
  • 317
  • 1
  • 6
  • 18

4 Answers4

3

Because it's a little more complex than a standard array sort, you'll need to use usort:

function compare_items( $a, $b ) {
    return $a->total_yes < $b->total_yes;
}


$arrayToSort = array ( 
    (object) array( 
        'id' => 1, 
        'question' => 'Action', 
        'specific_to_movie_id' => 1,
        'total_yes' => 4
    ), 
    (object) array( 
        'id' => 2,
        'question' => 'Created by DC Comics',
        'specific_to_movie_id' => 1,
        'total_yes' => 1
    ),
    (object) array( 
        'id' => 3,
        'question' => 'Christian Bale',
        'specific_to_movie_id' => 1,
        'total_yes' => 1
    ) 
);


usort($arrayToSort, "compare_items");

If you want to reverse the sort order, just change return $a->total_yes < $b->total_yes to use > (greater than) instead of < (less than)

adomnom
  • 564
  • 2
  • 9
2

you could use usort, like:

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

usort($your_array, "cmp");
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
0

You have object, therefore you need use [usort()][http://www.php.net/manual/en/function.usort.php]

usort($array, function($a, $b){
    if ($a->total_yes == $b->total_yes)
        return 0;
    return ($a->total_yes > $b->total_yes) ? -1 : 1;});
print_r($array);
Winston
  • 1,758
  • 2
  • 17
  • 29
0

You can use Usort() that use a specific compere function:

Definition and Usage

The usort() function sorts an array using a user-defined comparison function.

Syntax

usort(array,myfunction);

array -Required. Specifies the array to sort

myfunction-Optional. A string that define a callable comparison function. The comparison function must return an integer <, =, or > than 0 if the first argument is <, =, or > than the second argument

<?php

    function cmp($a, $b)
    {
        if ($a->total_yes == $b->total_yes) {
            return 0;
        }
        return ($a->total_yes < $b->total_yes) ? -1 : 1;
    }



    usort($array, "cmp");

    ?>
One Man Crew
  • 9,420
  • 2
  • 42
  • 51