For The Listing J example gives us a concrete explanation of how the PHP function array_multisort() works:
<ol class="dp-xml"> <li class="alt"> <span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=alt><SPAN><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>data</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>array</FONT></SPAN><SPAN>(array("id" =</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong></span><span> 1, "name" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> "Boney M", "rating" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 3), </span> </li> <li class=""> <span>array("id" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 2, "name" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> "Take That", "rating" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 1), </span> </li> <li class="alt"> <span>array("id" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 3, "name" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> "The Killers", "rating" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 4), </span> </li> <li class=""> <span>array("id" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 4, "name" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> "Lusain", "rating" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 3), </span> </li> <li class="alt"> <span>); foreach ($data as $</span><span class="attribute"><font color="#ff0000">key</font></span><span> =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> $value) { </span> </li> <li class=""><span>$name[$key] = $value['name']; </span></li> <li class="alt"><span>$rating[$key] = $value['rating']; </span></li> <li class=""><span>} </span></li> <li class="alt"><span>array_multisort($rating, $name, $data); print_r($data);</span></li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
Here, we simulate a row and column in the $data array array. I then used the PHP function array_multisort() to reorder the data set, first by rating, and then, if the ratings were equal, by name. Its output is as follows:
Array ([0] => Array
(
[id] => 2
[name] => Take That
[rating] = > 1
) [1] => Array
(
[id] => 1
[name] => Boney M
[rating] => 3
)
[2] => Array
(
[id] => 4
[name] => Lusain
[rating] => )
[3] => Array
(
[id] => 3
[name] => The Killers
[rating] => 4
)
)
The PHP function array_multisort() is one of the most useful functions in PHP, and it has a very wide range of applications. In addition, as you can see in the example, it can sort multiple unrelated arrays, it can also use one element as the basis for the next sort, and it can also sort database result sets.