php Seřadit pole podle klíče
$weight = [
'Pete' => 75,
'Benjamin' => 89,
'Jonathan' => 101
];
ksort($weight);
php Seřadit pole podle klíče
$inventory = [
['price' => 10.99, 'product' => 'foo 1'],
['price' => 5.99, 'product' => 'foo 2'],
['price' => 100, 'product' => 'foo 3'],
];
$price = array_column($inventory, 'price');
array_multisort($price, SORT_DESC, $inventory);
Seřadit pole podle hodnoty klíče v php
$inventory = array(
array("type"=>"fruit", "price"=>3.50),
array("type"=>"milk", "price"=>2.90),
array("type"=>"pork", "price"=>5.43),
);
$price = array_column($inventory, 'price');
array_multisort($price, SORT_DESC, $inventory);
php řadit pole podle konkrétního klíče
usort($array, function ($a, $b) {
return ($a['specific_key'] < $b['specific_key']) ? -1 : 1;
});
php Seřadit pole podle hodnoty a zachovat klíč
$weight = [
'Pete' => 75,
'Benjamin' => 309,
'Jonathan' => 101
];
asort($weight);
/*
weight is now:
Array
(
[Pete] => 75
[Jonathan] => 101
[Benjamin] => 309
)
To sort descending instead use: arsort
*/
php rsort Zachovat klíče
//Sort an array in reverse order and maintain index association
arsort($myArray)