All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

PHP Sort Multidimensional Array By Column

Last Updated : Mar 11, 2024

PHP Sort Multidimensional Array By Column

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;
  1. 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.
  2. Secondly, the <html&g; tag is used to indicate the beginning of an HTML document.
  3. 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.
  4. Both <head&g; and <title&g; tags are Paired tags. So, both have </head&g; and </title&g; ending tags respectively.
  5. Thirdly, the <body&g; tag is used to define the webpage body. All the contents to show on the website are written here.
  6. <h1&g; tag used to add heading here and also adding some inline CSS into it.
  7. Now we close the HTML file with </html&g; tag
  8. we write <?php tag to write PHP within it.
  9. Create a multidimensional array named $people with name and age as row and column.
  10. Using print_r() to display the array
  11. Create a function named sort_array() with formal argument of $a and $subkey.
  12. Use a foreach loop within the function with the strtolower() function to convert the strings into lowercase
  13. Using asort() to sort the array into ascending order.
  14. Calling the function with the key ‘name’
  15. Using print_r() to display the sorted array
  16. 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.

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪