Search
 
SCRIPT & CODE EXAMPLE
 

PHP

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);
Comment

php sort array by key

  $weight = [
    'Pete' => 75, 
    'Benjamin' => 89,
    'Jonathan' => 101
  ];  	
  ksort($weight);
Comment

php sort array by specific key


usort($array, function ($a, $b) {
  return ($a['specific_key'] < $b['specific_key']) ? -1 : 1;
});
Comment

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);
Comment

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).
Comment

sort array php by key

$price = array_column($inventory, 'price'); //price is a key for the sort
array_multisort($price, SORT_DESC, $inventory);
Comment

php array sort by key

ksort(array &$array, int $sort_flags = ?): int
Comment

php sort by key

//Laravel example
$products = collect($products)->sortBy('name')->toArray();
Comment

php sort by value then key

array_multisort(array_values($arrTags), SORT_DESC, array_keys($arrTags), SORT_ASC, $arrTags);
Comment

php order array by specific key

    function cmp($a, $b)
    {
        return strcmp($a->display_name, $b->display_name);
    }

    usort($blogusers, "cmp");

    foreach ($blogusers as $bloguser)
    {
        ...
Comment

PREVIOUS NEXT
Code Example
Php :: wp-config.php define home page 
Php :: center mode slick slider 
Php :: get current page slug 
Php :: clear file contents php 
Php :: php fpm status check 
Php :: call table name in model laravel 
Php :: smarty shorthand assign var 
Php :: php check if extension is installed 
Php :: php 7 to php 8 in ubuntu 20.04 
Php :: show php info 
Php :: laravel get latest record by date 
Php :: how can we use two php version in mac os 
Php :: laravel makehidden 
Php :: php 301 redirect 
Php :: save error cakephp 2 
Php :: Fetch Data From Database With MySQLI 
Php :: validate timestamp php 
Php :: placa de carro mercossul brasil 
Php :: php datetime create 
Php :: php switch 2 variables 
Php :: age php datetime 
Php :: destroy php variable 
Php :: php code to check if variable is null 
Php :: cart icon in woocommerce 
Php :: php import function from another file 
Php :: laravel human readable date 
Php :: php run python script 
Php :: laravel artisan progress bar 
Php :: different days in carbon laravel between different dates 
Php :: php get files in folder 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =