Return to Snippet

Revision: 56489
at March 30, 2012 22:54 by satie83


Initial Code
$people = array( 
    array("name"=>"Bob","age"=>8,"colour"=>"red"), 
    array("name"=>"Greg","age"=>12,"colour"=>"blue"), 
    array("name"=>"Andy","age"=>5,"colour"=>"purple")
);

$arrSortArray = array();

foreach($people as $person) {
    foreach($person as $key=>$value){
        if(!isset($arrSortArray[$key])){
            $arrSortArray[$key] = array();
        }
        $arrSortArray[$key][] = $value;
    }
}

$orderby = "name"; //change this to whatever key you want from the array

array_multisort($arrSortArray[$orderby],SORT_DESC,$people);

Initial URL
http://goo.gl/tyM6x

Initial Description
A nice way to do sorting of a key on a multi-dimensional array - DavidG

Initial Title
PHP - Sorting of a key on a multi-dimensional array

Initial Tags
php, sort, array

Initial Language
PHP