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 Push Multiple Values: A Complete Guide | StackCodee

PHP Array Push Multiple Values: A Complete Guide | StackCodee PHP Array Push Multiple Values: A Complete Guide 📅 November 8, 2025 ⏱️ 8 min read 🏷️ PHP, Arrays, Programming Welcome to StackCodee, your go-to resource for practical programming knowledge. In this comprehensive guide, we'll explore the various methods to push multiple values to a PHP array efficiently. Whether you're a beginner or an experienced developer, understanding these techniques will enhance your array manipulation skills in PHP. Understanding PHP Arrays PHP arrays are incredibly versatile data structures that can hold multiple values of different types. They can be indexed numerically or associatively with key-value pairs, and they can even function as lists, stacks, or queues. ...

PHPMyAdmin Localhost:8080 Setup Guide

PHPMyAdmin Localhost:8080 Setup Guide PHPMyAdmin Localhost:8080 Setup Guide Learn how to install, configure, and troubleshoot PHPMyAdmin running on localhost port 8080 with detailed examples and solutions to common problems. ⏱️ 10 min read 🏷️ PHPMyAdmin, MySQL, Localhost, Web Development Installation Steps 1 Install PHPMyAdmin Download and install PHPMyAdmin on your local server environment (XAMPP, WAMP, MAMP, or manual setup). # For Ubuntu/Debian systems sudo apt-get install phpmyadmin # For CentOS/RHEL systems sudo yum install phpmyadmin # Or download direc...

Understanding PHP Array Length with Practical Examples

PHP Array Length - How to Get Length of Array in PHP PHP Array Length – How to Get Length of Array in PHP When working with PHP, arrays are one of the most powerful and widely used data structures. Whether you are building a small project or a large-scale web application, knowing how to get length of array in PHP is crucial. In this article, we'll cover everything you need to know about finding the array length using different functions, best practices, and performance tips. What is an Array in PHP? An array in PHP is a special variable that can store multiple values under a single name. Instead of creating multiple variables, arrays help you organize and manipulate data efficiently. Arrays can be of three types: Indexed Arrays – arrays with numeric keys starting from 0. Associative Arrays – arrays with named keys. Multidimensional Arrays – arrays containing other ...