Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
448 views
in Technique[技术] by (71.8m points)

PHP sort array of arrays with key value

I have an array as shown below. I want to sort it with the srp value in ascending order. How can I do this in PHP. I am a beginner I think it can be done using usort() but am not sure how to do it.

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
        )

)

Desired Output:

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 looks like this:

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

    }


}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try below code, I hope it is helpful.

PHP Script.

<?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.
?>

Outputs.

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
        )

)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...