Skip to main content

What is array_multisort() in PHP?

 

📌 What is array_multisort() in PHP?

The array_multisort() function in PHP is a powerful tool used to sort multiple arrays at the same time or to sort a multidimensional array by one or more columns.
It sorts arrays in parallel while preserving the relationship between the elements in each array.


📌 Syntax of array_multisort()


array_multisort(array &$array1, array|int $array1_sort_order = SORT_ASC, array|int $array1_sort_flags = SORT_REGULAR, ...)
  • $array1, $array2, ... → Arrays to be sorted.

  • SORT_ASC / SORT_DESC → Optional. Specifies ascending or descending sort order.

  • SORT_REGULAR / SORT_NUMERIC / SORT_STRING → Optional. Specifies the type of sorting.


📌 How array_multisort() Works

  • It sorts the first array.

  • Reorders the other arrays based on the sorted order of the first.

  • Can also be used for multidimensional array sorting by extracting columns using array_column().


📌 Example 1: Sort Two Arrays Together


<?php $names = array("John", "Alex", "Mike"); $ages = array(25, 20, 30); // Sort by age ascending array_multisort($ages, SORT_ASC, $names); print_r($ages); print_r($names); ?>

Output:


Array ( [0] => 20 [1] => 25 [2] => 30 ) Array ( [0] => Alex [1] => John [2] => Mike )

📌 Example 2: Sort a Multidimensional Array by a Column


<?php $data = array( array("John", 25), array("Alex", 20), array("Mike", 30) ); // Get the age column $ages = array_column($data, 1); // Sort data by age array_multisort($ages, SORT_ASC, $data); print_r($data); ?>

Output:

Array ( [0] => Array ( [0] => Alex [1] => 20 ) [1] => Array ( [0] => John [1] => 25 ) [2] => Array ( [0] => Mike [1] => 30 ) )

📌 Example 3: Sort by Multiple Criteria


<?php $names = array("John", "Alex", "Mike", "John"); $ages = array(25, 20, 30, 20); // Sort by name ascending, then age descending array_multisort($names, SORT_ASC, $ages, SORT_DESC); print_r($names); print_r($ages); ?>

Output:


Array ( [0] => Alex [1] => John [2] => John [3] => Mike ) Array ( [0] => 20 [1] => 25 [2] => 20 [3] => 30 )

📌 Use Cases for array_multisort()

✅ Sort table-like data by multiple columns
✅ Sort related arrays while maintaining relationships
✅ Organize multidimensional datasets


📌 Final Thoughts

The array_multisort() function is one of PHP’s best tools for sorting when working with multiple arrays or multidimensional arrays.
It simplifies complex sorting operations and ensures that the data relationships are preserved during sorting.


📌 Official Documentation

For more details, visit:
👉 PHP array_multisort() Documentation

Comments

Popular posts from this blog

PHP array_column() Function

  <?php // An array that represents a possible record set returned from a database $a =  array (    array (      'id'  =>  5698 ,      'first_name'  =>  'Mehran' ,      'last_name'  =>  'Yaqoob' ,   ),    array (      'id'  =>  4767 ,      'first_name'  =>  'Muneeb' ,      'last_name'  =>  'Ahmad' ,   ),    array (      'id'  =>  3809 ,      'first_name'  =>  'Uzair' ,      'last_name'  =>  'Rajput' ,   ) ); $last_names = array_column($a,  'last_name' ); print_r($last_names); ?> Output: Array (   [0] => Yaqoob   [1] => Ahmad   [2] => Rajput ) Syntex: array_column( array ,  column_key ,  index_key ...

Mastering PHP's array_merge(): When to Use It (And When Not To)

Pros, Cons, and Best Practices PHP's  array_merge()  function is one of the most commonly used array functions in web development. Whether you're building a simple website or a complex web application, understanding how to effectively merge arrays can save you time and prevent headaches. In this comprehensive guide, we'll explore everything you need to know about  array_merge() , including its advantages, limitations, practical use cases, and alternatives. What is  array_merge()  in PHP? array_merge()  is a built-in PHP function that combines two or more arrays into a single array. The function takes multiple array arguments and returns a new array containing all the elements from the input arrays. Basic Syntax php array_merge ( array ... $arrays ) : array Simple Example php $array1 = [ 'a' , 'b' , 'c' ] ; $array2 = [ 'd' , 'e' , 'f' ] ; $result = array_merge ( $array1 , $array2 ) ; print_r ( $result ) ; /* O...