Sort PHP Multidimensional Array By Value

By | June 7, 2017

How to sort PHP multidimensional array by it’s value.

I have an array like this

Array
(
    [0] Array
        (
            [name] => Johny
            [age] => 14
        )

    [1] => Array
        (
            [name] => Ryan
            [age] => 12
        )

    [2] => Array
        (
            [name] => Jimmy
            [age] => 16
        )
)

And I want to sort this array by “age” key. If you have the same or similar problem here is the function to sort multidimensional array by value.

// that array
function sortById($a, $b) {
   return $a['age'] - $b['age']; //the key
}

usort($array, 'sortById');
//new sorted array
print_r($array);

Related Posts

agoda api

Gest Hotels List in A City With Agoda API

Get Hotel list in a city / area and or sub city / sub area Agoda data feed API. This api request will get data feed active hotels in a city or a sub city.

agoda api

Get Cities In a Country Agoda API

Agoda api tutorial to get data feed of cities in a selected country with php script

agoda api

Get List All Continents Agoda Api

Agoda API PHP tutorial & code script example to get list of all Agoda continents data feed.

agoda api

Get All Languages List Agoda Api

Agoda API Tutorial. Get list of all supported languages from Agoda API affilate. Agoda API affiliate supports multiple languages in their response data.

agoda api

Get Country List in A Continent Agoda Api

Agoda API Toturial to get country data feed, Agoda API request to get list of all countries from selected continent.