PHP对具有键值的数组排序

时间:2017-06-29 06:06:52

标签: php arrays sorting

我有一个如下所示的数组。我想按升序对 srp 值进行排序。我怎么能用PHP做到这一点。我是初学者,我认为可以使用 usort()来完成,但我不知道该怎么做。

Array
(
    [0] => stdClass Object
        (
            [_id] => 5911af8209ed4456d069b1d3
            [title] => Zen M4S(Silver)
            [mrp] => 0
            [srp] => 1900
        )

    [1] => stdClass Object
        (
            [_id] => 5911af8209ed4456d069b1d2
            [title] => Zen M4S(Silver)
            [mrp] => 0
            [srp] => 1000
        )

    [2] => stdClass Object
        (
            [_id] => 5911af8209ed4456d069b1d4
            [title] => JIVI X525(Red)
            [mrp] => 0
            [srp] => 1250
        )

)

期望输出:

Array
(
    [0] => stdClass Object
        (
            [_id] => 5911af8209ed4456d069b1d2
            [title] => Zen M4S(Silver)
            [mrp] => 0
            [srp] => 1000
        )

    [1] => stdClass Object
        (
            [_id] => 5911af8209ed4456d069b1d4
            [title] => JIVI X525(Red)
            [mrp] => 0
            [srp] => 1250
        )

    [2] => stdClass Object
        (
            [_id] => 5911af8209ed4456d069b1d3
            [title] => Zen M4S(Silver)
            [mrp] => 0
            [srp] => 1900
        )
)

My Class看起来像这样:

class TopDealsDAOMongoImpl implements TopDealsDAO{
    //put your code here
    public function getTopDeals() {
        $topDeals = json_decode(json_encode(MasterAffiliateProductMappingMongo::select("product_id","affiliate_id","title","our_product_id","mrp","srp","product_url","category_name","ID")->where('top_deal','true')->get()));
        function my_sort($a,$b)
        {
            if ($a->srp == $b->srp) return 0;
            return ($a->srp < $b->srp)?-1:1;
        }

        uasort($topDeals, 'my_sort'); 

    }


}

4 个答案:

答案 0 :(得分:1)

尝试以下代码, 我希望它有所帮助。

  

PHP脚本。

<?php
    // Static array.
    $array=array();
    $array[0]=array(
        '_id'=>'5911af8209ed4456d069b1d3',
        'title'=>'Zen M4S(Silver) 1',
        'srp'=>1900
    );
    $array[1]=array(
        '_id'=>'5911af8209ed4456d069b1d2',
        'title'=>'Zen M4S(Silver) 2',
        'srp'=>1000
    );
    $array[2]=array(
        '_id'=>'5911af8209ed4456d069b1d4',
        'title'=>'Zen M4S(Silver) 1',
        'srp'=>1250
    );

    // For acending sorting.
    function asc_sort_json($array1,$array2){
        $on = 'srp';
        if ($array1[$on] == $array2[$on]) {
            return 0;
        }
        return ($array1[$on] < $array2[$on]) ? -1 : 1;
    }
    // For decending sorting.
    function desc_sort_json($array1,$array2){
        $on = 'srp';
        if ($array1[$on] == $array2[$on]) {
            return 0;
        }
        return ($array1[$on] < $array2[$on]) ? 1 : -1;
    }

    $array_json = json_encode($array); // encode array in json
    $array_json1 = json_decode($array_json); // decode json from array because get json object
    $array_json2 = json_decode(json_encode($array_json1),true); // parse json into array

    usort($array_json2, "asc_sort_json"); // Call function for acending order.
    $array_json2 = json_decode(json_encode($array_json2)); // Array to json.

    $array_json1 = json_encode($array); // encode array in json
    $array_json2 = json_decode($array_json1); // decode json from array because get json object
    $array_json3 = json_decode(json_encode($array_json2),true); // parse json into array

    usort($array_json3, "desc_sort_json"); // Call function for decending order.
    $array_json4 = json_decode(json_encode($array_json2)); // Array to json.
?>
  

输出

echo "<pre>";
print_r($array_json2);
echo "<pre>";
print_r($array_json4);

// Asc 
Array
(
    [0] => stdClass Object
        (
            [_id] => 5911af8209ed4456d069b1d2
            [title] => Zen M4S(Silver) 2
            [srp] => 1000
        )

    [1] => stdClass Object
        (
            [_id] => 5911af8209ed4456d069b1d4
            [title] => Zen M4S(Silver) 1
            [srp] => 1250
        )

    [2] => stdClass Object
        (
            [_id] => 5911af8209ed4456d069b1d3
            [title] => Zen M4S(Silver) 1
            [srp] => 1900
        )

)

// Desc
Array
(
    [0] => stdClass Object
        (
            [_id] => 5911af8209ed4456d069b1d2
            [title] => Zen M4S(Silver) 2
            [srp] => 1000
        )

    [1] => stdClass Object
        (
            [_id] => 5911af8209ed4456d069b1d4
            [title] => Zen M4S(Silver) 1
            [srp] => 1250
        )

    [2] => stdClass Object
        (
            [_id] => 5911af8209ed4456d069b1d3
            [title] => Zen M4S(Silver) 1
            [srp] => 1900
        )

)

答案 1 :(得分:0)

尝试一下:

// Sort array
uasort($array, 'cmp');

function cmp($a, $b) {
   if ($a['srp'] == $b['srp']) {
      return 0;
   }
   // Return in acsending order
   return ($a['srp'] < $b['srp']) ? -1 : 1;
}

了解详情:Uasort

答案 2 :(得分:0)

对于简单数组

function my_sort($a,$b)
{
    if ($a['srp'] == $b['srp']) return 0;
    return ($a['srp'] < $b['srp'])?-1:1;
}

uasort($array, 'my_sort'); // replace $array with your variable

对于StdObject语法将是:

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

uasort($array, 'my_sort'); // replace $array with your variable

答案 3 :(得分:-1)

使用usort并在其中传递用户定义功能。

$tmp =  new stdClass;
$tmp->_id="5911af8209ed4456d069b1d3";
$tmp->title="Zen M4S(Silver)";
$tmp->mrp=0;
$tmp->srp=1900;

$tmp1 =  new stdClass;
$tmp1->_id="5911af8209ed4456d069b1d2";
$tmp1->title="Zen M4S(Silver)";
$tmp1->mrp=0;
$tmp1->srp=1000;

$tmp2 =  new stdClass;
$tmp2->_id="5911af8209ed4456d069b1d4";
$tmp2->title="JIVI X525(Red)";
$tmp2->mrp=0;
$tmp2->srp=1250;

$new_array = array($tmp,$tmp1,$tmp2);
function sort_arr($a, $b)
{
    return ($a->srp<$b->srp)?-1:1;
}

usort($new_array, "sort_arr");
var_dump($new_array);

从你的编辑中你正在使用课程,所以你可以这样做:

class TopDealsDAOMongoImpl implements TopDealsDAO{
    //put your code here
    public function getTopDeals() {
        $topDeals = json_decode(json_encode(MasterAffiliateProductMappingMongo::select("product_id","affiliate_id","title","our_product_id","mrp","srp","product_url","category_name","ID")->where('top_deal','true')->get()));


        uasort($topDeals, array($this, "sort_arr")); 

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


}

O / P:

array (size=3)
  0 => 
    object(stdClass)[2]
      public '_id' => string '5911af8209ed4456d069b1d2' (length=24)
      public 'title' => string 'Zen M4S(Silver)' (length=15)
      public 'mrp' => int 0
      public 'srp' => int 1000
  1 => 
    object(stdClass)[3]
      public '_id' => string '5911af8209ed4456d069b1d4' (length=24)
      public 'title' => string 'JIVI X525(Red)' (length=14)
      public 'mrp' => int 0
      public 'srp' => int 1250
  2 => 
    object(stdClass)[1]
      public '_id' => string '5911af8209ed4456d069b1d3' (length=24)
      public 'title' => string 'Zen M4S(Silver)' (length=15)
      public 'mrp' => int 0
      public 'srp' => int 1900