<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-comment">// Introductory content unrelated to code</span></span><span>
</span><span><span class="hljs-comment">// Author: ChatGPT</span></span><span>
</span><span><span class="hljs-comment">// Date: 2025-08-23</span></span><span>
<p></span>// ---------------------- Main Content Separator ----------------------</p>
<p>/**</p>
<ul>
<li>
<p>Title: How to Efficiently Use PHP’s array_multisort() Function for Multidimensional Array Sorting</p>
</li>
<li></li>
<li>
<p>When working with multidimensional arrays, developers often need to sort data based on one or multiple fields.</p>
</li>
<li>
<p>PHP’s built-in array_multisort() function provides an efficient way to accomplish this.</p>
</li>
<li>
<p>It can sort a single array, as well as multiple arrays or multidimensional arrays in sync.</p>
</li>
<li></li>
<li>
<ol>
<li>
<p>Basic Syntax of array_multisort()</p>
</li>
</ol>
</li>
<li>
<p>array_multisort(array &$array1, array $array2 = [], array $... = [], int $sorting_order = SORT_ASC, int $sorting_flags = SORT_REGULAR): bool</p>
</li>
<li></li>
<li>
<ul>
<li>
<p>&$array1: Required. The first array to sort.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>$array2, ... : Optional. Additional arrays to maintain correspondence with the first array.</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>$sorting_order: Sorting order, commonly SORT_ASC (ascending) or SORT_DESC (descending).</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>$sorting_flags: Sorting type, e.g., SORT_REGULAR (normal comparison), SORT_NUMERIC (numeric comparison), SORT_STRING (string comparison).</p>
</li>
</ul>
</li>
<li></li>
<li>
<ol start="2">
<li>
<p>Applying to Multidimensional Arrays</p>
</li>
</ol>
</li>
<li>
<p>When working with datasets composed of multidimensional arrays (such as database query results), you often need to sort by a specific key.</p>
</li>
<li></li>
<li>
<p>Example:<br>
*/</p>
</li>
</ul>
<p>$data = [<br>
["id" => 3, "name" => "Alice", "score" => 85],<br>
["id" => 1, "name" => "Bob", "score" => 92],<br>
["id" => 2, "name" => "Cindy", "score" => 78],<br>
];</p>
<p>// Extract the columns to sort<br>
$ids = array_column($data, "id");<br>
$scores = array_column($data, "score");</p>
<p>// Sort by score descending, then by ID ascending if scores are equal<br>
array_multisort($scores, SORT_DESC, </span>$ids, SORT_ASC, </span>$data);</p>
<p>print_r($data);</p>
<p>/**</p>
<ul data-is-last-node="" data-is-only-node="">
<li>
<p>Output:</p>
</li>
<li>
<p>Array</p>
</li>
<li>
<p>(</p>
</li>
<li>
(
[id] => 1
[name] => Bob
[score] => 92
)
[1] => Array
(
[id] => 3
[name] => Alice
[score] => 85
)
[2] => Array
(
[id] => 2
[name] => Cindy
[score] => 78
)
)
Tips and Considerations
Using array_column() helps quickly extract the sorting column and avoids loops.
You can specify multiple sorting conditions, such as "first by score, then by ID".
array_multisort() will change the original array indexes; if you need to preserve indexes, use uasort() first.
For large datasets, array_multisort() performs better than manually implementing sorting logic.
Conclusion
array_multisort() is a powerful tool for sorting multidimensional arrays, especially when sorting by multiple criteria.
Mastering its usage can significantly improve code simplicity and execution efficiency.
*/