A PHP one-dimensional array can be sorted with functions such as sort (), Asort (), Arsort (), but the ordering of PHP's two-dimensional arrays needs to be customized.
The following function is to sort a given two-dimensional array according to the specified key value, and first look at the function definition:
functionArray_sort ($arr,$keys,$type= ' ASC '){ $keysvalue=$new _array=Array(); foreach($arr as $k=$v){ $keysvalue[$k] =$v[$keys]; } if($type= = ' ASC '){ Asort($keysvalue); }Else{ Arsort($keysvalue); } Reset($keysvalue); foreach($keysvalue as $k=$v){ $new _array[$k] =$arr[$k]; } return $new _array; }
It can sort the two-dimensional arrays by the specified key values, or you can specify ascending or descending sorting (the default is ascending), and the usage example:
$array=Array( Array(' name ' = ' phone ', ' brand ' = ' Nokia ', ' Price ' =>1050),Array(' name ' = ' notebook computer ', ' brand ' = ' Lenovo ', ' Price ' =>4300),Array(' name ' = ' razor ', ' brand ' = ' Philips ', ' Price ' =>3100),Array(' name ' = = ' treadmill ', ' brand ' = ' three and ' pine stone ', ' price ' =>4900),Array(' name ' = ' watch ', ' brand ' = ' casio ', ' Price ' =>960),Array(' name ' = ' + ' LCD tv ', ' Brand ' = ' sony ', ' Price ' =>6299),Array(' name ' = ' laser printer ', ' brand ' = ' hp ', ' Price ' =>1200));$ShoppingList= Array_sort ($array, ' Price '); For $array, this two-dimensional array is sorted by ' price ' from low to highPrint_r($ShoppingList);