[Solved] PHP sort array of arrays with key value


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
            PHP sort array of arrays with key value => Zen M4S(Silver) 2
            [srp] => 1000
        )

    [1] => stdClass Object
        (
            [_id] => 5911af8209ed4456d069b1d4
            PHP sort array of arrays with key value => Zen M4S(Silver) 1
            [srp] => 1250
        )

    [2] => stdClass Object
        (
            [_id] => 5911af8209ed4456d069b1d3
            PHP sort array of arrays with key value => Zen M4S(Silver) 1
            [srp] => 1900
        )

)

// Desc
Array
(
    [0] => stdClass Object
        (
            [_id] => 5911af8209ed4456d069b1d2
            PHP sort array of arrays with key value => Zen M4S(Silver) 2
            [srp] => 1000
        )

    [1] => stdClass Object
        (
            [_id] => 5911af8209ed4456d069b1d4
            PHP sort array of arrays with key value => Zen M4S(Silver) 1
            [srp] => 1250
        )

    [2] => stdClass Object
        (
            [_id] => 5911af8209ed4456d069b1d3
            PHP sort array of arrays with key value => Zen M4S(Silver) 1
            [srp] => 1900
        )

)

solved PHP sort array of arrays with key value