PHP を学習しているとき、PHP の多次元配列のソートの問題に混乱するかもしれません。この記事では、PHP の多次元配列のソートの原理を詳しく紹介します。 PHP では、多次元配列に対してより複雑なソートを実行することもできます。たとえば、最初に共通キーを使用してネストされた配列をソートし、次に別のキーに基づいてソートします。これは、SQL の ORDER BY ステートメントを使用して複数のフィールドを並べ替えるのとよく似ています。どのように動作するかをよりよく理解するために、与えられた例を注意深く見てください:
<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 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> </span></li> <li class=""> <span>array_multisort($rating, $name, $data); print_r($data);</span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
<ol class="dp-xml"> <li class="alt"><span><span>Array ([0] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> Array </span></span></li> <li class=""><span>( </span></li> <li class="alt"> <span>[id] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 2 </span> </li> <li class=""> <span>[name] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> Take That </span> </li> <li class="alt"> <span>[rating] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 1 </span> </li> <li class=""> <span>) [1] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> Array </span> </li> <li class="alt"><span>( </span></li> <li class=""> <span>[id] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 1 </span> </li> <li class="alt"> <span>[name] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> Boney M </span> </li> <li class=""> <span>[rating] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 3 </span> </li> <li class="alt"><span>) </span></li> <li class=""> <span>[2] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> Array </span> </li> <li class="alt"><span>( </span></li> <li class=""> <span>[id] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 4 </span> </li> <li class="alt"> <span>[name] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> Lusain </span> </li> <li class=""> <span>[rating] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 3 </span> </li> <li class="alt"><span>) </span></li> <li class=""> <span>[3] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> Array </span> </li> <li class="alt"><span>( </span></li> <li class=""> <span>[id] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 3 </span> </li> <li class="alt"> <span>[name] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> The Killers </span> </li> <li class=""> <span>[rating] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 4 </span> </li> <li class="alt"><span>) </span></li> <li class=""><span>) </span></li> </ol>