Sort (); Ascending
sort the array;
11 <?PHP2233$fruits=Array("Lemon", "orange", "banana", "apple" );44Sort($fruits );55foreach($fruits as $key=$val ) {66Echo"Fruits[".$key. "] = " .$val. "\ n" ;77 }8899?>Ten10//Output Results One11 AFruits[0] =Apple -FRUITS[1] =Banana -FRUITS[2] =Lemon theFRUITS[3] = Orange
Rsort (); descending
- Reverse Array sorting
This function array assigns the new key name to the element in. This will delete the original key name instead of just reordering the key name.
<?PHP$fruits=Array("Lemon", "orange", "banana", "apple" );Rsort($fruits );foreach($fruits as $key=$val ) { Echo"$key=$val\ n " ;}?>//The above routines will output:0 =Orange1 =Lemon2 =Banana3 = Apple
Arsort (); ascending
Reverse-Sort an array and maintain an index relationship
1<?PHP2 $fruits=Array("D" = "lemon", "a" and "Orange", "B" and "Banana", "C" and "Apple" );3 Arsort($fruits );4 foreach($fruits as $key=$val ) {5 Echo"$key=$val\ n " ;6 }7?>8//The above routines will output:9 TenA =Orange OneD =Lemon Ab =Banana -c = Apple
Asort (); descending
Sorts and maintains an index relationship with an array;
1<?PHP2 $fruits=Array("D" = "lemon", "a" and "Orange", "B" and "Banana", "C" and "Apple" );3 Asort($fruits );4 foreach($fruits as $key=$val ) {5 Echo"$key=$val\ n " ;6 }7?>8 //The above routines will output:9 Tenc =Apple Oneb =Banana AD =Lemon -A = Orange
Ksort (); ascending
Arrays are sorted by key name.
1<?PHP2 $fruits=Array("D" = "lemon", "a" and "Orange", "B" and "Banana", "C" and "Apple" );3 Ksort($fruits );4 foreach($fruits as $key=$val ) {5 Echo"$key=$val\ n " ;6 }7?>8 //The above routines will output:9 TenA =Orange Oneb =Banana Ac =Apple -D = Lemon
Krsort (); descending
reverse order of array by key name
1<?PHP2 $fruits=Array("D" = "lemon", "a" and "Orange", "B" and "Banana", "C" and "Apple" );3 Krsort($fruits );4 foreach($fruits as $key=$val ) {5 Echo"$key=$val\ n " ;6 }7?>8 //The above routines will output:9 TenD =Lemon Onec =Apple Ab =Banana -A = Orange