Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php sort multidimensional array

$inventory = array(
   array("type"=>"Fruit", "price"=>3.50),
   array("type"=>"milk", "price"=>2.90),
   array("type"=>"Pork", "price"=>5.43),
);

$prices = array_column($inventory, 'price');
$inventory_prices = array_multisort($prices, SORT_DESC, $inventory);

$types = array_map(strtolower, array_column($inventory, 'type'));
$inventory_types = array_multisort($types, SORT_ASC, $inventory);
Comment

php sort multidimensional array

array_multisort(array_map(function($element) {
      return $element['order'];
  }, $array), SORT_ASC, $array);

print_r($array);
Comment

sort multi array php

		$keys = array_column($array, 'Price');

		array_multisort($keys, SORT_ASC, $array);
	
		print_r($array);
Comment

sort multidimensional array php by key

$people= array(
    array("age"=>54,"first_name"=>"bob","last_name"=>"Dillion"),
    array("age"=>22,"first_name"=>"darah","last_name"=>"Harvard"),
    array("age"=>31,"first_name"=>"ahuck","last_name"=>"Bartowski"),
);

echo '<PRE>';
print_r($people);


$keys = array_column($people, 'first_name');
print_r($keys);

array_multisort($keys, SORT_ASC, $people);

print_r($people);
Comment

php sort multidimensional array by value

usort($myArray, function($a, $b) {
    return $a['order'] <=> $b['order'];
});
Comment

php sort multidimensional array by value

function sortByOrder($a, $b) {
    return $a['order'] - $b['order'];
}

usort($myArray, 'sortByOrder');
Comment

php sort multidimensional array by key

function buildSorter($key) {
    return function ($a, $b) use ($key) {
        return strnatcmp($a[$key], $b[$key]);
    };
}

usort($array, buildSorter('key_b'));
Comment

Sorting Multi Dimensional Array in PHP

//Products Array
$products = [
    [
        'id' => 1,
        'title' => 'Laptop',
        'stock' => 12,
    ],[
        'id' => 2,
        'title' => 'Mobile',
        'stock' => 30,
    ],[
        'id' => 3,
        'title' => 'USB Cable',
        'stock' => 5,
    ],[
        'id' => 4,
        'title' => 'Power Bank',
        'stock' => 53,
    ],[
        'id' => 5,
        'title' => 'Mobile Charger',
        'stock' => 28,
    ],
];

//Sort array by stock in descending order
$sorted_products = sort_array_by_key($products, 'stock');
print_r($sorted_products);


//Function to sort array by key
function sort_array_by_key($array, $sort_key){
    $key_array = array_column($array, $sort_key);
    array_multisort($key_array, SORT_DESC, $array); //or SORT_ASC
    return $array;
}
Comment

PREVIOUS NEXT
Code Example
Php :: google client php get inbox messages 
Php :: How to set a comment on table using Laravel Schema 
Php :: current page link using php 
Php :: how to debug in php 
Php :: laravel force user logout 
Php :: get php to send email from form 
Php :: how import the impliment countable php 
Php :: php hide decimals if zero 
Php :: php remove last character from string if comma 
Php :: check empty laravel blade 
Php :: laravel get query in normal sql without bindings 
Php :: symfony get api paths 
Php :: get table name of model laravel inside the model 
Php :: copy php array to javascript 
Php :: laravel blade @guest 
Php :: codeigniter count rows 
Php :: get single row in codeigniter 
Php :: laravel eloquent randomise data from database 
Php :: wordpress get permalink in loop 
Php :: php remove everything after a specific character 
Php :: order by sum() laravel 
Php :: remove link from product name in woocommerce cart 
Php :: php how to rename a file before saving it 
Php :: How to copy all files from one folder to another in PHP? 
Php :: wordpress get perma link 
Php :: php nan 
Php :: convert text to slug php 
Php :: downgrade php version vagrant 
Php :: laravel 8 check if null or empty 
Php :: twig trim space 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =