【问题标题】:PHP sort array of arrays with key valuePHP对具有键值的数组进行排序
【发布时间】:2017-12-02 16:52:00
【问题描述】:

我有一个如下所示的数组。我想用 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
        )
)

我的班级看起来像这样:

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

    }


}

【问题讨论】:

标签: php arrays sorting


【解决方案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
        )

)

【讨论】:

    【解决方案2】:

    试试这个:

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

    【讨论】:

    • [ErrorException] uasort() 期望参数 2 为有效回调,未找到函数“my_sort”或无效函数名
    • 您能在此处发布返回该错误的代码吗? my_sort 不在我的示例中。
    • 也试过你的:uasort($topDeals, 'cmp'); var_dump($topDeals);函数 cmp($a, $b) { if ($a['srp'] == $b['srp']) { return 0; } // 按升序返回 return ($a['srp']
    • 好的,你想要答案Sort array of objects by object fields
    【解决方案3】:

    对于简单数组

    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
    

    【讨论】:

    【解决方案4】:

    使用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
    

    【讨论】:

    • [ErrorException] 为 foreach() 提供的参数无效
    • 一定是不同的东西。在这个 q/a 中没有 foreach 循环
    • usort($topDeals, function($a, $b) { return $b->srp - $a->srp; }); var_dump($topDeals);这有效
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-06
    • 1970-01-01
    • 2021-02-15
    相关资源
    最近更新 更多