1<?php
2$fruit = array("apple","banana","mango","orange","strawbary");
3
4sort($fruit); //arrange in ascending order
5echo "<pre>";
6print_r($fruit);
7
8rsort( $fruit); //sort in descending order
9foreach($fruit as $val)
10{
11 echo $val."<br>";
12}
13
14$girl = array("krisha"=>20,"yashvi"=>30,"ritu"=>4,"pinal"=>80);
15asort($girl); //sort in ascending order according to value
16print_r($girl);
17
18ksort($girl); //sort in ascending order according to key
19print_r($girl);
20
21arsort($girl); //sort in descending order according to value
22print_r($girl);
23
24krsort($girl); //sort in descending order according to key
25print_r($girl);
26?>
1
2<?php
3$fruits = array("lemon", "orange", "banana", "apple");
4rsort($fruits);
5foreach ($fruits as $key => $val) {
6 echo "$key = $val\n";
7}
8?>
9
10
1<?php
2$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
3asort($fruits);
4foreach ($fruits as $key => $val) {
5 echo "$key = $val\n";
6}
7?>
8//Would output:
9c = apple
10b = banana
11d = lemon
12a = orange
1$letters=array("b","a","c");
2arsort($letters); //referse sort an array ("c","a","b")
1PHP function sort(array &$array, int $flags) bool
2---------------------------------------------
3Sort an array
4
5Parameters:
6array--$array--The input array.
7int--$flags--[optional] The optional second parameter sort_flags may be used to modify the sorting behavior using these values.
8Sorting type flags: SORT_REGULAR - compare items normally (don't change types).
9
10Returns: true on success or false on failure.