One or one-D array sorting
1.1. Sort sorted in ascending order of an array
$arrSort=array(3,56,6,8,9,7,6); Sort ($arrSort); Print_r ($arrSort);
Output:
Array
(
[0] = 3
[1] = 6
[2] = 6
[3] = 7
[4] = 8
[5] = 9
[6] = 56
)
1.2, Rsort in descending order of the array
$arrRSort=array(3,56,6,8,9,7,6); Rsort ($arrRSort); Print_r ($arrRSort);
Output:
Array
(
[0] = 56
[1] = 9
[2] = 8
[3] = 7
[4] = 6
[5] = 6
[6] = 3
)
1.3. Asort the associative array in ascending order according to the value
$arrASort=[ "Name" = "Wangk", "blog" = "rhythmk.cnblogs.com", "Address" = "Hunan", "Company" = "abcdef"; Asort ($arrASort); Print_r ($arrASort);
Output:
(
[Company] = abcdef
[Address] = Hunan
[Blog] = rhythmk.cnblogs.com
[Name] = Wangk
)
1.4. Ksort the associative array in ascending order according to the key
$arrKSort=[ "Name" = "Wangk", "blog" = "rhythmk.cnblogs.com", "Address" = "Hunan", "Company" = "abcdef"; Ksort ($arrKSort); Print_r ($arrKSort);
Output:
(
[Address] = Hunan
[Blog] = rhythmk.cnblogs.com
[Company] = abcdef
[Name] = Wangk
)
1.5. Arsort the associative array in descending order based on the value
$arrARSort=[ "Name" = "Wangk", "blog" = "rhythmk.cnblogs.com", "Address" = > "Hunan", "Company" and "abcdef"]; Arsort ($arrARSort); Print_r ($arrARSort);
Output:
Array
(
[Name] = Wangk
[Blog] = rhythmk.cnblogs.com
[Address] = Hunan
[Company] = abcdef
)
1.6. Krsort the associative array in descending order according to the key
$arrKRSort=[ "Name" = "Wangk", "blog" = "rhythmk.cnblogs.com", "Address" = > "Hunan", "Company" and "abcdef"]; Krsort ($arrKRSort); Print_r ($arrKRSort);
Output:
Array
(
[Name] = Wangk
[Company] = abcdef
[Blog] = rhythmk.cnblogs.com
[Address] = Hunan
)
Two, multidimensional array custom sorting
$arrUSort=[ ["id" =>10, "title" = "ABCD"], ["id" =>11, "title" = "DVBCD"], ["id" =>2, "title" = "CDDGBCD"], ["id" =>5, "title" = "EA"], ["id" =>8, "title" = "Aceg"]]; functionUserdefinedsort ($a,$b){ if($a["title"]>$b["title"]) { return1; }Else{ return0; }}Usort($arrUSort, ' Userdefinedsort ');Print_r($arrUSort);
Output:
Array
(
[0] = = Array
(
[id] = 10
[Title] = ABCD
)
[1] = = Array
(
[id] = 8
[Title] = Aceg
)
[2] = = Array
(
[id] = 2
[Title] = CDDGBCD
)
[3] = = Array
(
[id] = 11
[Title] = DVBCD
)
[4] = = Array
(
[id] = 5
[title] = = ea
)
)
Reference:
Http://www.w3school.com.cn/php/php_arrays_sort.asp
PHP array sorting