1//php 7+
2usort($inventory, function ($item1, $item2) {
3 return $item1['price'] <=> $item2['price'];
4});
1$inventory = [
2 ['price' => 10.99, 'product' => 'foo 1'],
3 ['price' => 5.99, 'product' => 'foo 2'],
4 ['price' => 100, 'product' => 'foo 3'],
5
6];
7
8$price = array_column($inventory, 'price');
9
10array_multisort($price, SORT_DESC, $inventory);
1$price = array();
2foreach ($inventory as $key => $row)
3{
4 $price[$key] = $row['price'];
5}
6array_multisort($price, SORT_DESC, $inventory);
1// take an array with some elements
2$array = array('a','z','c','b');
3// get the size of array
4$count = count($array);
5echo "<pre>";
6// Print array elements before sorting
7print_r($array);
8for ($i = 0; $i < $count; $i++) {
9 for ($j = $i + 1; $j < $count; $j++) {
10 if ($array[$i] > $array[$j]) {
11 $temp = $array[$i];
12 $array[$i] = $array[$j];
13 $array[$j] = $temp;
14 }
15 }
16}
17echo "Sorted Array:" . "<br/>";
18print_r($array);
19