PHP Sort Array от Key
$weight = [
'Pete' => 75,
'Benjamin' => 89,
'Jonathan' => 101
];
ksort($weight);
Xenophobic Xenomorph
$weight = [
'Pete' => 75,
'Benjamin' => 89,
'Jonathan' => 101
];
ksort($weight);
//php 7+
usort($inventory, function ($item1, $item2) {
return $item1['price'] <=> $item2['price'];
});
To PHP sort array by key, you should use:
ksort() (for ascending order) or krsort() (for descending order).
To PHP sort array by value, you will need functions:
asort() and arsort() (for ascending and descending orders).
<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
asort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n";
}
?>
//Would output:
c = apple
b = banana
d = lemon
a = orange
<?php
$array = array("id" => 8, "id" =>1, "id" =>3, "id"=>2, "id" => 12, "id" =>19);
print_r($array->orderBy("id"));
?>
$price = array();
foreach ($inventory as $key => $row)
{
$price[$key] = $row['price'];
}
array_multisort($price, SORT_DESC, $inventory);