Answers for "array key sort php"

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
10

php sort array by key

$weight = [
    'Pete' => 75, 
    'Benjamin' => 89,
    'Jonathan' => 101
  ];  	
  ksort($weight);
Posted by: Guest on December-04-2019
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
2

php sort array of array by key

$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);
Posted by: Guest on August-25-2020
2

php array sort by key value

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).
Posted by: Guest on October-23-2020
0

php array sort by key

ksort(array &$array, int $sort_flags = ?): int
Posted by: Guest on September-06-2021

Browse Popular Code Answers by Language