1 $weight = [
2 'Pete' => 75,
3 'Benjamin' => 89,
4 'Jonathan' => 101
5 ];
6 ksort($weight);
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$price = array();
2foreach ($inventory as $key => $row)
3{
4 $price[$key] = $row['price'];
5}
6array_multisort($price, SORT_DESC, $inventory);
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