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 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 :: how to print something in php 
Php :: newline not working php 
Php :: php tutorial 
Php :: using get in laravel blade 
Php :: Invalid credentials. symfony 
Php :: php number multiple of 
Php :: woocommerce set default shipping country 
Php :: change php version on ubuntu 
Php :: how to get value in to radio button php 
Php :: Non-static method called statically php 
Php :: check if column has value in laravel eloquent 
Php :: ErrorException symlink(): No such file or directory 
Php :: seo_url.php location opencart 
Php :: laravel migration mediumint length 
Php :: namecheap shared cpanel change php version for subdomain 
Php :: append variable into string php 
Php :: no routes.php in http folder 
Php :: why the laravel project have many cache 
Php :: laravel controller subfolder 
Php :: how to reverse a string in php 
Php :: laravel eloquent with query parameter 
Php :: php get today at 3pm 
Php :: how to back the page laravel where the scorll is 
Php :: Before Action methoond in Yii 1 controller 
Php :: php get woocommerce attribute from database 
Php :: check date is in the last 24 hours? 
Php :: laravel collection isNotEmpty 
Php :: php get file from another server 
Php :: blade check user role laravel 
Php :: laravel collection pop 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =