简体   繁体   中英

How to sort array value using the key in PHP

i need one help.i am sorting array using the key value,i need if one key value will same then it should come in alphabetically order again.Let me to explain my code below.

{"data":[
    {"subcat_id":"9","subcat_name":"SUSHI","order":"9"},
    {"subcat_id":"20","subcat_name":"APPETIZERS","order":"6"},
    {"subcat_id":"6","subcat_name":"CHINESE","order":"4"},
    {"subcat_id":"26","subcat_name":"BRUNCH","order":"4"},
    {"subcat_id":"17","subcat_name":"ENTREES","order":"3"},
    {"subcat_id":"25","subcat_name":"HAPPY HOUR","order":"2"}
]} 

here if order value will same ( lets say-order->4 ),then it should come the alphabetically as per subcat_name ( BRUNCH,CHINESE ) like this.

You must create groups by order id and then sort them.

$json = '{"data":[
    {"subcat_id":"9","subcat_name":"SUSHI","order":"9"},
    {"subcat_id":"20","subcat_name":"APPETIZERS","order":"6"},
    {"subcat_id":"6","subcat_name":"CHINESE","order":"4"},
    {"subcat_id":"26","subcat_name":"BRUNCH","order":"4"},
    {"subcat_id":"17","subcat_name":"ENTREES","order":"3"},
    {"subcat_id":"25","subcat_name":"HAPPY HOUR","order":"2"}
]}';

$data = json_decode($json);

$ordersGroups = array();
foreach ($data->data as $orderData) {
    if (!isset($ordersGroups[$orderData->order])) {
        $ordersGroups[$orderData->order] = array($orderData);
    } else {
        $ordersGroups[$orderData->order][] = $orderData;
    }
}

$ordersGroups = array_map(function ($orders) {
    usort($orders, function ($a, $b) {
        return strcmp($a->subcat_name, $b->subcat_name);
    });
    return $orders;
}, $ordersGroups);

$data->data = array_reduce($ordersGroups, function ($carry, $orderGroup) {
    return array_merge($carry, $orderGroup);
}, array());

$json = json_encode($data);

This may work:

$array = //decode your JSON as an array here

$innerComparison = function ($v1, $v2) {
       return strcmp($v1["subcat_name"],$v2["subcat_name"]);
};   

$mainComparison = function ($v1, $v2) use ($innerComparison)  {
       if ($v1["order"] === $v2["order"]) {
             return $innerComparison($v1,$v2);
       } else {
          return $v1["order"] < $v2["order"]:-1:1; 
       }

};

usort($array["data"], $mainComparison);

You can use usort() . Assuming your data is stored in the $obj object:

usort
(
    $obj->data,
    function( $a, $b )
    {
        if( $a->order == $b->order ) return strcmp( $a->subcat_name, $b->subcat_name );
        return $a->order - $b->order;
    }
);

above function sort $obj->data according to your request.

eval.in demo

usort() sort an array using a custom function, that receive two elements of array as arguments: the returned function value must be an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

In your case, if ->order keys are equals, we use strcmp() to return sort value based on ->subcat_name , otherwise we return the difference between two ->order values.


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM