<?php$fruits=array("d"=>"lemon","a"=>"orange","b"=>"banana","c"=>"apple");asort($fruits);foreach($fruitsas$key=>$val){echo"$key = $val
";}?>
//Would output:
c = apple
b = banana
d = lemon
a = orange
// take an array with some elements$array=array('a','z','c','b');// get the size of array$count=count($array);echo"<pre>";// Print array elements before sortingprint_r($array);for($i=0;$i<$count;$i++){for($j=$i+1;$j<$count;$j++){if($array[$i]>$array[$j]){$temp=$array[$i];$array[$i]=$array[$j];$array[$j]=$temp;}}}echo"Sorted Array:"."<br/>";print_r($array);
sort()- sort arrays in ascending order
rsort()- sort arrays in descending order
asort()- sort associative arrays in ascending order, according to the value
ksort()- sort associative arrays in ascending order, according to the key
arsort()- sort associative arrays in descending order, according to the value
krsort()- sort associative arrays in descending order, according to the key
PHPfunctionsort(array&$array,int$flags)bool---------------------------------------------
Sort an array
Parameters:array--$array--The input array.int--$flags--[optional] The optional second parameter sort_flags may be used to modify the sorting behavior using these values.
Sorting type flags:SORT_REGULAR- compare items normally(don't change types).
Returns:true on success orfalse on failure.