1$inventory = array(
2
3 array("type"=>"fruit", "price"=>3.50),
4 array("type"=>"milk", "price"=>2.90),
5 array("type"=>"pork", "price"=>5.43),
6
7);
8$price = array_column($inventory, 'price');
9array_multisort($price, SORT_DESC, $inventory);
1 $weight = [
2 'Pete' => 75,
3 'Benjamin' => 89,
4 'Jonathan' => 101
5 ];
6 ksort($weight);
1
2usort($array, function ($a, $b) {
3 return ($a['specific_key'] < $b['specific_key']) ? -1 : 1;
4});
5
1$weight = [
2 'Pete' => 75,
3 'Benjamin' => 309,
4 'Jonathan' => 101
5];
6asort($weight);
7/*
8weight is now:
9Array
10(
11 [Pete] => 75
12 [Jonathan] => 101
13 [Benjamin] => 309
14)
15To sort descending instead use: arsort
16*/
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);
1To PHP sort array by key, you should use:
2 ksort() (for ascending order) or krsort() (for descending order).
3
4To PHP sort array by value, you will need functions:
5 asort() and arsort() (for ascending and descending orders).