PHP - Using array_multisort() to sort multidimensional arrays

Introduction

You can use array_multisort() to sort multidimensional arrays.

You only pass in one array.

The function then sorts the array by the first element of each nested array, then by the second element of each nested array, and so on.

The order of the elements in the nested array is untouched.

The following code illustrates how array_multisort() sorts a two-dimensional array.

Demo

<?php

$myBooks = array(
      array(//from ww  w .  j  av  a 2  s.  c o  m
       "title"=> "Java",
       "author"=> "John A",
       "pubYear"=>  2018
   ),
      array(
       "title"=> "SQL",
       "author"=> "John A",
       "pubYear"=>  2000
   ),
      array(
       "title"=> "Database",
       "author"=> "Franz B",
       "pubYear"=>  2017
   ),
             array(
              "title"=> "Json",
              "author"=> "Oracle",
              "pubYear"=>  2001
          ),
             array(
              "title"=> "MySQL",
              "author"=> "Charles D",
              "pubYear"=>  2002
          ),
        );

           array_multisort($myBooks);
           echo"";
           print_r($myBooks);
           echo"";

?>

Result

Related Topic