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 sort associative array by specific value

$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 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 :: laravel require vendor autoload 
Php :: laravel old value or default 
Php :: laravel datatable format date column 
Php :: php remove stop words from string 
Php :: wordpress featured image show 
Php :: laravel query select from table where id != to another table id 
Php :: associative array sorting by value in php 
Php :: var dump php look clear 
Php :: phpcs 
Php :: check if a string contains a substring in php 
Php :: laravel auth user_id 
Php :: how to create wordpress shortcodes 
Php :: percentage in php 
Php :: phpspreadsheet read xlsx 
Php :: send value from one page to another in php 
Php :: laravel db query 
Php :: save an image use php 
Php :: Check if a String Starts With a Specified String in PHP 
Php :: php use variable as object key 
Php :: php array order by value 
Php :: php fix array keys 
Php :: php remove all whitespace from a string 
Php :: for in php 
Php :: laravel/ui for laravel 7 
Php :: php exception import 
Php :: PHP File Read Modes 
Php :: laravel collection keys 
Php :: php remove path from string url 
Php :: datetime validation in laravel 
Php :: php remove html tag 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =