php sort multidimensional array by key

Solutions on MaxInterview for php sort multidimensional array by key by the best coders in the world

showing results for - "php sort multidimensional array by key"
Angelo
16 Aug 2017
1function sortByAge($a, $b) {
2    return $a['age'] > $b['age'];
3}
4$people=[
5    ["age"=>54,"first_name"=>"Bob","last_name"=>"Dillion"],
6    ["age"=>22,"first_name"=>"Sarah","last_name"=>"Harvard"],
7    ["age"=>31,"first_name"=>"Chuck","last_name"=>"Bartowski"]
8];
9
10usort($people, 'sortByAge'); //$people is now sorted by age (ascending)
Juan Sebastián
14 Sep 2017
1array_multisort(array_map(function($element) {
2      return $element['order'];
3  }, $array), SORT_ASC, $array);
4
5print_r($array);
Louella
12 Jun 2016
1		$keys = array_column($array, 'Price');
2
3		array_multisort($keys, SORT_ASC, $array);
4	
5		print_r($array);
Carl
23 Jun 2020
1$inventory = array(
2   array("type"=>"Fruit", "price"=>3.50),
3   array("type"=>"milk", "price"=>2.90),
4   array("type"=>"Pork", "price"=>5.43),
5);
6
7$prices = array_column($inventory, 'price');
8$inventory_prices = array_multisort($prices, SORT_DESC, $inventory);
9
10$types = array_map(strtolower, array_column($inventory, 'type'));
11$inventory_types = array_multisort($types, SORT_ASC, $inventory);
Lucia
14 Nov 2016
1<?php
2function sortByPrice($a, $b){
3	return $a['price'] > $b['price'];
4}
5
6$items = [
7    ['label' => 'cake', 'name' => 'Cake', 'price' => 150],
8    ['label' => 'pizza', 'name' => 'Pizza', 'price' => 250],
9    ['label' => 'puff', 'name' => 'Veg Puff', 'price' => 20],
10    ['label' => 'samosa', 'name' => 'Samosa', 'price' => 14]
11];
12
13//Sort by Price
14usort($items, 'sortByPrice');
15//print_r($items);
16
17print "<br/> After Sort by Price printing: <br/>";
18foreach($items as $item){
19	print $item['name']." ".$item['price']."<br/>";
20}
21$newArray = array_column($items, 'price', 'name');
22
23// find max, min, and toal sum of array
24$totalExp = array_sum(array_column($items, 'price', 'name'));
25$maxPrice  = max(array_column($items, 'price', 'name'));
26$minPrice  = min(array_column($items, 'price', 'name'));
27
28print "Total Expenses : ".$totalExp."<br/>";
29print "What is Costly Item : ".$maxPrice.' ('.array_search($maxPrice, $newArray).")<br/>";
30print "What is Cheap Item : ".$minPrice.' ('.array_search($minPrice, $newArray).")<br/>";
31
32?>
Spencer
22 Feb 2018
1function buildSorter($key) {
2    return function ($a, $b) use ($key) {
3        return strnatcmp($a[$key], $b[$key]);
4    };
5}
6
7usort($array, buildSorter('key_b'));
queries leading to this page
sort 2d array phpphp array sort sub fieldorder 3 multidimensional array phpphp multi sortphp sort order multidimensional arraysort php multidimensional arrayphp sub arrayphp sort multi dimensional array by valueorder nested arrayarray multisort by key overwrite phpphp asort multidimensional arrayphp sort array with multiple by 2sort array of arrays phpphp sort multidimensional array with a key usortphp sort array2d array sort phpphp order multi arrayphp sort subarrays by lengtharrange in ascending order to multidimensional array by value phpsort multi array phpsort 2d simple string array by specific order array phpsort a multidimensional array in phpphp sort array multidimensoinal array php sort multidimensional array by column namemultidimensional array array sort by valuephp simply sort a compund arrayphp sort multidimensional array indexlaravel array multisortphp array multisort 28 29sort array by value multidimensional phpphp sort multiple array with valuemultisort 28 29 phpphp merge two multidimensional array and sort by value alphabeticallymultisort array in phporder array by value php 2dphp array multi sort by keyphp sort multidimensional array two columnsorder multidimensional array by some value phphow to sort an array of double digits in phpphp sort array with multiple valuesmultisort php array columnphp order a bidimensional array usortsort multidimensionna larray phpsubarray phpphp sort nested array by keyorder multidimensional array php by valuephp sort multidimensional array by value high to lowsort multidimensional array by another array values in phpphp sort vs array multisortphp sort 2d array by valuephp sort multidimensional array by value descendingmulti sort phpsort multidimensional array by item phpsort a multidimensional arrayarray multisort with 2 sorting keys phpphp sort multiple array with value isarraymultiple order phpsort multidimensional array by the hishest value phpphp sorting multidimensional arrayarray multisort letterphp usort multidimensional arrayhow to sort a 2d array based on on ecolumn phpphp array sort by value multidimensionalphp sort multidimensional array by arrayphp sort array by subarray valuephp sort by name in mulphp sub listsort array by time multidimensional phpsort uone array in multiple array valuephp array multi sort by valuephp array sort by multiple valuephp arraymultisort sort by multidimensional value phpsort multidimensional array php by keyarray multisortphp sort multidimensional array by value uknown keysort multidimensional array php no indexphp sort a multidimensional arrayphp array sort based on nested array valuesubstsr phpphp sort array of arraysphp sort multidimensional array by keyphp sort array multidimensional by valuesort multiple dimensional in phptwo dimensional array sort in phpsort value by key php multidimensional arraymultiarray sort phpphp muti array how to sortphp sort array by nested valuephp sort array multiple columnssort float values on column multi dimensional array in phpmultidimensional array sortphp sort multidimensional array by value ascphp order array multidimensionalphp order by value 2d arraysort multiple array phpphp order multidimensional array by key valuesort by key in multidimensional array phpphp sort associative array third level multisortmultidimensional array php sortphp sort multi dimensional arrayphp sort multidimensional array value by dateorder multidimensional array phpphp sort subarray from 2d arraywrite down function to sort a two dimensional array using phpphp sort multidimensional array by valuesort nested array php by valuephp sort multidimensional array by key alphabeticallyorder 2d array on valluephp array multi sort does not worksimple array sorting function in phpsort multidimensional array php assending ordersort multiple arrays against eachother phpphp sort multidimensional array by multiple columnsphp sort multidimensional array by number of keys2d array sort in phpusort php multidimensional arrayphp sort multidimensional array by value non case insensitivephp array multisort array1 sort order as variablesort multidimensional object array by string value php ascphp multisortphp sort multidimensional associative array by valuephp multi array sort by keyphp sort multidimensional array using another arrayphp sort by value in multidimensional arraymultisort in phpsort 2d array php by value of keyphp sort multidimensional arrayarray multisort modulecan i apply sort on multi dimensional array4th dimensional array sort with multidimensional array phpphp sort multidimensional array by priority fielphp sort multidimensional array by array columnmultiple array sorting by value in phparray sort key multidimensionalin array php multidimensionalsort multidimensionnal array phpphp custom sort multidimensional arraysort multidimensional array by number of elements phpphp sort multidimensional array by columnorder multidimensional array php ascorder multiple arraysort array by sub item php 5cphp multidimension array sort byphp sort nested array by valuesort multi dimension array phpphp sort bidimensional arrayhow to sort value of multidimensional array in phpphp sort multidementional by value or arrayphp sort multidimensional array by another arraysort multidimensional object array by string value phporder multidimensional array php by value and ascsort multidimensional array by index phphow to sort two dimensional array in phpsort array 2d phpsort multidimensional associative array phphow to sort multidimentional array phpsort multidimensional array by key valuephp sort array multidimensional array using value alphabeticallysort asc in phpphp order multidimensional array by highest valuesort and array in php by multiple nested valuesphp array sort two by valuehow to sort 2d array in phpsort arrays based on sub arrays inner value phpphp sort multidimensional array by key descendingphp sort array by multiple valuessort php multidimensional array based on filter selectionsort array on the basis of key value multidimensional phpsort multidimensional array by another array values in php site 3astackoverflow comsort array php multidimensionalsort multidimensional array in descending order phpphp sort multidimensional array by specific key string valuearray multi sort phpmultisort order by value in phpmultidimensional array sort phpsort multidimensional array php for amountphp sort multidimensional array by value of keychange order of multidimensional array phpphp multidimensional array sort element php how to sort a multidimensional array by valuearray multisort 2 arrays against a mainphp order 2d array byrearrange multi dimentional array based on valuephp array sorting multidimentiaonalmultidimensional array sort in phpsort ascending order wise 3d array basis of column in phpsort multidimensional array in php with valuesphp array sort custom orderphp properly sort a two dimensional frist by columnsort multidimensional array by value phpsort multidimensional array in phpsorting multidimensional arrayphp 2 dimension array sort by key valuephp sort two dimensional arraysort multidimensional array by another array string value in phpsort nested array in phpphp multidimensional array sort by value stringsort array multidimensional php valuearray multisort objects phpsort multidimensional array php by two valuesphp array diff sorti le manquantmulti sort decending phpphp array multisort by value then by stringphp sort array by key multidimensionalphp properly sort a two dimensional by columnsort multidimesional array based on particular key in phphow to sort multidimensional array in phpsort multidimensional array phparray sort multidimensional phpsub arrays in phpphp ordenar array por varios campossort column php 5 6php multiple sortusort multidimensional array phpsub array from array phparray sort multiphp sort multidimensional array customarray muktisortsort multidimensional nested array phpsort multidimensional array php by key valuesorting an array phpsort a multi level associative array phpphp sort multi array by keyhow to sort an multidimensional array in phphow to sort a multidimensional array in phpphp multidimensional array order element to firstsort an array using a column of the array phpphp sorting multidimensional array by keysort multidimensional array php by valuephp array multisort multidimensionalphp order array by value in sub arraysort array php by value multidimensionalsubtract array from array phpmultidimensional array sort by valuephp sort array by value multidimensionalsorting multisort array in phpphp sort multidimensional array multiple columnsphp sort multi arrayphp array sort by 2 valuesarray multisort string matchphp properly sort a two dimensional array by columnphp sort array by multiple keyssort associative 2d array in phpphp sort multidimensional array by sting valuearray multisort phpphp sort multi array by valuephp array sort multidimensionalphp multidimensional array sortorder array multidimensional phpphp sort multidimensional array by 2 valuesarray multisort sampleselect sub array phpphp multidimensional array sort by column descphp sort 2d array by a value in a named subarrayin array multidimensional phparray msort in laravel arrayphp array multisort multiple columnsphp sorting multi dimensional arraysort array by subarray value phpphp sort multidimensional array one linetwo dimensional array sort phpphp multidimensional array order by valuephp multi sort string and numberphp sort multidimentional array with value of decimal pointphp sort multidimensional associative array by value alphabeticallythree dimensional array sorting phphow to re arrange multi dimensional arraysort column in array phpmultidimensional array sortingphp sort multidimensional array by column ascendingphp sort array by columnphp sort multidimensional array by specific keysort array multiple conditions phpmultidimensional array sort example php 8order multidimensional array in php by valuephp array multisortphp sort value multidimensional arrayphp sort multidimensional array by specific key valuesort multidimensional array php 5cphp sort two dimensional array by max valuesort multidimensional array by key value phpphp sort multidimensional array content ascending by keyphp multidimensional array sort by specific valuefunction to sort two dimensional array in php by two valuessort multidimensional array by column phpphp sort array by sub array keysort a multidimensional array in php based on particular key valuephp sort 2 arrays based on valuesort dimensional array and divide in multiple array as subsequesnt in phphow to sort 2 d array phpmultidimensional array sorting examplephp sort multidimensional array by multiple values3rd dimensional array sort with multidimensional array phpphp sort multidimensional array by value ascendingmulti level array sorting in phparray flip multidimensional phpmultidimensional array sort by desc in phpphp sort multidimensional array by true falsemultidimensional array sort by desc value in phpphp multidimensional array sort by key valuephp sort array 2 dimensional arraraysort multidimensional array of strings and many numbers phpphp sort multidimensional array by 2 valuesphp array sort by key multidimensionalsubstr 28 29 with array phpsort multidimesional array in php based on given keyphp sort array two dimensionalsort two dimensional array phpsort multidimensional array order by another array values in phpphp array multi sort 1 and 10 not workingphp natural sort multidimensional arrayphp order multidimensional array by valueordenar array php multidimensionalsorting multidimensianal arrays phpsort multidimensional arraymultisort php sort asc 7csort natural 7csort flag casesort a2d array phparray sort php multidimensionalarray sort by value php multilanguagephp array multisortphp possible sub arrayphp muiltiy arry sort by array valuephp sort by multiple valuesmultidimensional array sorting in phpsort multidimensional array by key phpphp sort array by value multidimensional arrayphp how to sort multidimensional arrayphp arry multisortacessando array multidimensional phpsort php array multidimensionalphp sort multidimensional object by valuephp sort column of multidimensional arrayhow to sort multidimensional array by case insensitive in phpreorder array columns in phpphp sort multidimensional array by 2nd level keysort multidimensional array php no keyphp sort multidimensional inner arrayphp sort multidimensional array by lengthphp sort multidimensional array by value alphabeticallyphp sort array by multiple fieldsphp sort multidimensional array by first valuephp sort array sub valuephp array sort multidimensional by valuesort array php deminsing valuesort multidimensional array according to another array in phpsort a 2d array in phpphp multisort multidimensional array1 dimensional array sorting in php examplesorting a multidimensional array in phpsort multidimensional array by keys phphow to sort an multidimensional array by key value in phpphp sort multidimensional array by column valuesort multi array by value phpsort by column array phparray multisorthow to sort multidimensional associative array in phpphp sort multidimensional array get specific columnphp array column sort array multisortphp merge and sort multi arrayphp sort field in multidimensional arrayphp multi array sortphp sort json arrayphp sort arrays with subarray valuessort multidimensional array php with keysub array from a array phprearrange array columns in phpphp sort multidimensional array by indexmulti dimensional array order by value phpphp sort two dimensional array by columnsorting array with mutiple keys phpsort a compund arrayphp sort multi level arrayphp sort associative array by multiple valuesphp multidimensional array sort by columnphp two dimensional array sort by valuefunction arraymultisortsorting multidimensional array phpphp multidimensional array sort by indexalfabetic sort multidimensional array phpsort php multidimensional array by keysorting array phpsort array by numeric multidimensional phpphp sort array by value in subarrayphp array sort by columnphp sorting multidimensional array by valuephp sort multidimensional array by key value descendingphp sort multidimensional array by custom valuesort array php by value 2darray sort key multidimensional phpsort multiarray by value phpfunction to sort two dimensional array in php ksort multidimensional array phparray multidimensional php order by valuesorting multidimensional array in phpphp order array by subarray valuemultidimensional array sort by value in phphow to sort multidimensional array in php according to valuesort dimensional array in phpphp sort by key multidimensional arrayphp sort multidimensional array by 2php sort multidimensional sortsort php multidimensional array based on variable valuesort php multidimensional array based on filtersort array by value php multiphp sort 2d arrayarray column with array sort phparray multisort 28 29 phparray multisort phpsort arry with tow values phporder array by value nested phpsort multidimensional array by particular valuesort mutitple mutitdimensional array in phparray multisort key valuearray multisort in phpphp sort array multidimensionalphp sort multidimensional array of string case insensitivesorting n dimensional array based on one valuesort multidimensional array php laravelarray mulitsortphp sort desc multidimensional array by valuephp sort array by value of subarraysort associative multidimensional array phpphp sort multidimensional array by key valueorder multidimensional array by value phpmultidimentionnal array sort phpphp sort multidimensional arrayssearch multidimensional array key phporder key array multiple phpphp array column array multisortdynamic array multidimensional sorting phpphp sort multidimensional array by array keyphp how to create multidimensional arrayarray multisort 28array map 29 in phparray column array multisortphp reorder multidimensional array by valuearray multisort couldnot be converted to floatphp sorting of a key on a multi dimensional array without havingsort multidimensional array by valuetwo keys multisort phpafter array multisort the element values are changed why 3fphp subarrayphp sort multidimensional array by key