📌 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()
-
$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
✅ Output:
📌 Example 2: Sort a Multidimensional Array by a Column
✅ Output:
📌 Example 3: Sort by Multiple Criteria
✅ Output:
📌 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
Post a Comment