如何根据[num_of_offers]列的最高值打印数组?

时间:2015-12-22 18:09:34

标签: php

如何按“num_of_offers”键的值对此数组进行排序?目前我正在得到这样的阵列:

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 
                        ) 
          )

但我想要这样的数组:

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 
                 ) 
    )

5 个答案:

答案 0 :(得分:4)

使用uasort()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;
    }
 });

在这种情况下,您实际上不太可能想要使用uasort,因为这会将索引保留在新排序的数组中,我认为您不希望这样。如果您的索引是有意义的字符串或沿着这些行的某些内容,uasort()非常有用。

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

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

答案 1 :(得分:0)

如果您的数组是数据库查询的结果,请让数据库进行排序(通过将ORDER BY num_of_offers附加到查询中)。

答案 2 :(得分:0)

尝试这种方式,未经测试,如果需要,请进行更改

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

usort($your_data, "cmp");

答案 3 :(得分:0)

使用预定义的回调函数进行排序,您可以尝试(未测试):

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' );

答案 4 :(得分:0)

如果你有

,你可以轻松搞定
$array = 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 
                    ) 
      )

你可以使用array_reverse($ array);像这样

$array = array_reverse($array);
echo '<pre>';
print_r($array);
echo '</pre>';

See This