PHP Sort Multidimensional Array By Column
Last Updated : Mar 11, 2024
In this article we will show you the solution of PHP sort multidimensional array by column, an array is a collection of the homogenous element.
A multidimensional array has more than one dimension. We can sort it by column using the php asort() method.
Asort() in this php function, we can sort any array by ascending order.
Step By Step Guide On PHP Sort Multidimensional Array By Column :-
In the example below, we sorted a multidimensional array by column using php. Let us see the code first.
<!DOCTYPE html&g; <html lang = " en " &g; <head&g; <meta charset = " UTF - 8" &g; <meta http-equiv = " X-UA-Compatible " content = " IE=edge " &g; <meta name = " viewport " content = " width = device-width , initial-scale = 1.0 " &g; <title&g; php sort multidimensional array by column </title&g; </head&g; <body&g; <h1 style=" color : rgb(113, 221, 113) ;"&g; TALKERSCODE </h1&g; <h2&g; php sort multidimensional array by column </h2&g; <?php $people = array ( '1' =&g; array ('name' =&g; 'Peter', 'age' =&g; '30') , '2' =&g; array ('name' =&g; 'Mary', 'age' =&g; '24') , '3' =&g; array ('name' =&g; 'Stefan', 'age' =&g; '65') , '4' =&g; array ('name' =&g; 'Thomas', 'age' =&g; '34') , '5' =&g; array ('name' =&g; 'Rose', 'age' =&g; '70') , '6' =&g; array ('name' =&g; 'Anna', 'age' =&g; '26') , '7' =&g; array ('name' =&g; 'Arther', 'age' =&g; '19') , '8' =&g; array ('name' =&g; 'George', 'age' =&g; '41') , '9' =&g; array ('name' =&g; 'Henry', 'age' =&g; '32') , '10' =&g; array ('name' =&g; 'Lily', 'age' =&g; '18') ) ; function Sort_array ($a,$subkey) { foreach ($a as $k =&g; $v) { $b[$k] = strtolower($v[$subkey]) ; } asort ($b) ; foreach ($b as $key =&g; $val) { $c[] = $a[$key] ; } return $c ; } print_r ($people) ; echo "<br&g;" ; $sorted_list = Sort_array ($people, 'name') ; print_r ($sorted_list) ; ?&g; </body&g; </html&g;
- First, we write <! DOCTYPE html&g; which we used as an instruction to the web browser about what version of HTML file is written in.
- Secondly, the <html&g; tag is used to indicate the beginning of an HTML document.
- As above now the <head&g; tag is used to contain information about the web page. In this tag, a <title&g; tag is used which helps us to specify a webpage title.
- Both <head&g; and <title&g; tags are Paired tags. So, both have </head&g; and </title&g; ending tags respectively.
- Thirdly, the <body&g; tag is used to define the webpage body. All the contents to show on the website are written here.
- <h1&g; tag used to add heading here and also adding some inline CSS into it.
- Now we close the HTML file with </html&g; tag
- we write <?php tag to write PHP within it.
- Create a multidimensional array named $people with name and age as row and column.
- Using print_r() to display the array
- Create a function named sort_array() with formal argument of $a and $subkey.
- Use a foreach loop within the function with the strtolower() function to convert the strings into lowercase
- Using asort() to sort the array into ascending order.
- Calling the function with the key ‘name’
- Using print_r() to display the sorted array
- Closing the php code with ?&g; tag.
Conclusion :-
At last, here in conclusion, here we can say that with this article’s help, we know how to sort a multidimensional array by column using PHP.
I hope this article on PHP sort multidimensional array by column helps you and the steps and method mentioned above are easy to follow and implement.