All TalkersCode Topics

Follow TalkersCode On Social Media

PHP Sort Multidimensional Array By Key

Last Updated : Jul 1, 2023

PHP Sort Multidimensional Array By Key

In this article we will show you the solution of PHP sort multidimensional array by key, here to achieve desired result we need to use array_coulmn(), array_multisort() helps a lot.

The array_column() function returns the values from single column of the input array and verified by array key names.

The array_multisort() function sorts given array either descending or ascending as per specified input. Also through this you can sort more than two array, which is executed one by one.

Step By Step Guide On PHP Sort Multidimensional Array By Key :-

As foremost step, you should define multidimensional array based on your wish. Shown example array values can modifiable not mandatory and you need to pass the array variable to array_column() method.

There as second parameter define any key name in your array like shown in example ‘code’.

It returns stored on one variable that means retrieved single column values. Then define array_multisort() method with required parameters such as separated column array, sorting type, array.

Then you get result, which updated directly to the original array. So just print array at last with the help of print_r() and var_dump() also help you to print array values.

array_multisort($key,SORT_ASC, $mdarr);
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
  3. We defined array variable ‘$mdarr’ with three set of values and same as earlier said change this if you want. This array has two coumns ‘code, mark’ with three different values.
  4. In this array to sort column ‘code’ wise inside array_column() method first we passing array ‘$mdarr’ along within single quotes given column name ‘code’. Then returning result of collected single column stored on variable ‘$key’.
  5. Then array_multisort() method defined with appropriate inputs ‘$key, SORT_ASC-refers ascending order sort, $mdarr’. Now you accomplished your chore so need to print array ‘$mdarr’ to verify result.
  6. Using print_r() we displayed array on the output it may vary if you changed array values otherwise you obtain result shown example with ascending order sort. During execution ensure whether you started the server first or not, if not you cannot see result properly.

Conclusion :-

In conclusion, now we are able to do multidimensional sort by key order in php.

When work with php we need to create and process php files at server location and then we need to start the server before execute the program.

When we executing this program on browser it will print ‘Array ( [0] => Array ( [code] => HTML [mark] => 95 ) [1] => Array ( [code] => Java [mark] => 75 ) [2] => Array ( [code] => Php [mark] => 80 ) )’ at webpage browser.

I hope this article on PHP sort multidimensional array by key helps you and the steps and method mentioned above are easy to follow and implement.