Answers for "php sort array by value without changing key"

PHP
1

sort array by key value in 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);
Posted by: Guest on June-03-2021
1

php sort array by specific key

usort($array, function ($a, $b) {
  return ($a['specific_key'] < $b['specific_key']) ? -1 : 1;
});
Posted by: Guest on January-22-2021
1

php sort array by value and keep key

$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
*/
Posted by: Guest on June-09-2021
0

php rsort retain keys

//Sort an array in reverse order and maintain index association
arsort($myArray)
Posted by: Guest on September-14-2020

Code answers related to "php sort array by value without changing key"

Browse Popular Code Answers by Language