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//php 7+
2usort($inventory, function ($item1, $item2) {
3 return $item1['price'] <=> $item2['price'];
4});
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
2<?php
3
4$fruits = array("lemon", "orange", "banana", "apple");
5sort($fruits);
6foreach ($fruits as $key => $val) {
7 echo $val;
8}
9/*
10OUTPUT:
11apple
12banana
13lemon
14orange
15*/
16?>
17
18
1$price = array();
2foreach ($inventory as $key => $row)
3{
4 $price[$key] = $row['price'];
5}
6array_multisort($price, SORT_DESC, $inventory);
1function aasort (&$array, $key) {
2 $sorter=array();
3 $ret=array();
4 reset($array);
5 foreach ($array as $ii => $va) {
6 $sorter[$ii]=$va[$key];
7 }
8 asort($sorter);
9 foreach ($sorter as $ii => $va) {
10 $ret[$ii]=$array[$ii];
11 }
12 $array=$ret;
13}
14
15aasort($your_array,"order");
16