PHP에는 강력한 함수 라이브러리 세트가 있습니다. 각 배열 정렬 함수의 구체적인 사용법을 살펴보겠습니다. 아래 스크린샷은 PHP7.0 매뉴얼에서 가져온 것입니다.
bool sort ( array &$array [, int $sort_flags = SORT_REGULAR ] )
배열을 제외한 두 개의 매개변수 두 번째 선택적 매개변수 sort_flags는 선택적 유형 플래그를 통해 정렬 동작을 변경합니다. <code><span><span><?php</span><span>$auto</span> = <span>array</span>(<span>"bens2"</span>,<span>"honda3"</span>,<span>"BMW4"</span>,<span>"aens1"</span>,<span>"BMW5"</span>); sort(<span>$auto</span>,SORT_STRING|SORT_FLAG_CASE);<span>//不区分大小写排序字符串</span><span>foreach</span>(<span>$auto</span><span>as</span><span>$key</span>=><span>$val</span>) { <span>echo</span><span>"auto["</span>.<span>$key</span>.<span>"]="</span>.<span>$val</span>.<span>"\n"</span>; <span>//输出auto[0]=BMW4 auto[1]=BMW5 auto[2]=aens1 auto[3]=bens2 auto[4]=honda3</span> }</span></span></code>
<code><span><span><?php</span><span>$auto</span> = <span>array</span>(<span>"bens2"</span>,<span>"honda3"</span>,<span>"BMW4"</span>,<span>"aens1"</span>,<span>"BMW5"</span>); rsort(<span>$auto</span>,SORT_STRING|SORT_FLAG_CASE);<span>//不区分大小写排序字符串</span><span>foreach</span>(<span>$auto</span><span>as</span><span>$key</span>=><span>$val</span>) { <span>echo</span><span>"auto["</span>.<span>$key</span>.<span>"]="</span>.<span>$val</span>.<span>"\n"</span>; <span>//输出auto[0]=honda3 auto[1]=BMW5 auto[2]=BMW4 auto[3]=bens2 auto[4]=aens1</span> }</span></span></code>
<code><span><span><?php</span><span>$auto</span> = <span>array</span>(<span>"a"</span>=><span>"bens2"</span>,<span>"b"</span>=><span>"honda3"</span>,<span>"c"</span>=><span>"BMW4"</span>,<span>"d"</span>=><span>"aens1"</span>,<span>"e"</span>=><span>"BMW5"</span>); asort(<span>$auto</span>,SORT_STRING|SORT_FLAG_CASE);<span>//不区分大小写排序字符串</span><span>foreach</span>(<span>$auto</span><span>as</span><span>$key</span>=><span>$val</span>) { <span>echo</span><span>"auto["</span>.<span>$key</span>.<span>"]="</span>.<span>$val</span>.<span>"\n"</span>; <span>//输出auto[d]=aens1 auto[a]=bens2 auto[c]=BMW4 auto[e]=BMW5 auto[b]=honda3</span> }</span></span></code>
<code><span><span><?php</span><span>$auto</span> = <span>array</span>(<span>"a"</span>=><span>"bens2"</span>,<span>"b"</span>=><span>"honda3"</span>,<span>"c"</span>=><span>"BMW4"</span>,<span>"d"</span>=><span>"aens1"</span>,<span>"e"</span>=><span>"BMW5"</span>); arsort(<span>$auto</span>,SORT_STRING|SORT_FLAG_CASE);<span>//不区分大小写排序字符串</span><span>foreach</span>(<span>$auto</span><span>as</span><span>$key</span>=><span>$val</span>) { <span>echo</span><span>"auto["</span>.<span>$key</span>.<span>"]="</span>.<span>$val</span>.<span>"\n"</span>; <span>//输出auto[b]=honda3 auto[e]=BMW5 auto[c]=BMW4 auto[a]=bens2 auto[d]=aens1</span> }</span></span></code>
<code><span><span><?php</span><span>$auto</span> = <span>array</span>(<span>"b"</span>=><span>"bens2"</span>,<span>"a"</span>=><span>"honda3"</span>,<span>"d"</span>=><span>"BMW4"</span>,<span>"c"</span>=><span>"aens1"</span>,<span>"e"</span>=><span>"BMW5"</span>); ksort(<span>$auto</span>,SORT_STRING|SORT_FLAG_CASE);<span>//不区分大小写排序字符串</span><span>foreach</span>(<span>$auto</span><span>as</span><span>$key</span>=><span>$val</span>) { <span>echo</span><span>"auto["</span>.<span>$key</span>.<span>"]="</span>.<span>$val</span>.<span>"\n"</span>; <span>//输出auto[a]=honda3 auto[b]=bens2 auto[c]=aens1 auto[d]=BMW4 auto[e]=BMW5</span> }</span></span></code>
<code><span><span><?php</span><span>$auto</span> = <span>array</span>(<span>"b"</span>=><span>"bens2"</span>,<span>"a"</span>=><span>"honda3"</span>,<span>"d"</span>=><span>"BMW4"</span>,<span>"c"</span>=><span>"aens1"</span>,<span>"e"</span>=><span>"BMW5"</span>); ksort(<span>$auto</span>,SORT_STRING|SORT_FLAG_CASE);<span>//不区分大小写排序字符串</span><span>foreach</span>(<span>$auto</span><span>as</span><span>$key</span>=><span>$val</span>) { <span>echo</span><span>"auto["</span>.<span>$key</span>.<span>"]="</span>.<span>$val</span>.<span>"\n"</span>; <span>//输出auto[a]=honda3 auto[b]=bens2 auto[c]=aens1 auto[d]=BMW4 auto[e]=BMW5</span> }</span></span></code>
<code><span><span><?php</span><span>$auto</span> = <span>array</span>(<span>"b"</span>=><span>"bens2"</span>,<span>"e"</span>=><span>"bens20"</span>,<span>"a"</span>=><span>"bens31"</span>,<span>"d"</span>=><span>"bens41"</span>,<span>"c"</span>=><span>"bens1"</span>,<span>"e"</span>=><span>"bens15"</span>); sort(<span>$auto</span>,SORT_STRING|SORT_FLAG_CASE);<span>//不区分大小写排序字符串</span><span>echo</span><span>"sort排序:<br>"</span>; <span>foreach</span>(<span>$auto</span><span>as</span><span>$key</span>=><span>$val</span>) { <span>echo</span><span>"auto["</span>.<span>$key</span>.<span>"]="</span>.<span>$val</span>.<span>"<br>"</span>; } <span>$auto1</span> = <span>array</span>(<span>"b"</span>=><span>"bens2"</span>,<span>"e"</span>=><span>"bens20"</span>,<span>"a"</span>=><span>"bens31"</span>,<span>"d"</span>=><span>"bens41"</span>,<span>"c"</span>=><span>"bens1"</span>,<span>"e"</span>=><span>"bens15"</span>); natsort(<span>$auto1</span>);<span>//参数只有一个</span><span>echo</span><span>"natsort排序:<br>"</span>; <span>foreach</span>(<span>$auto1</span><span>as</span><span>$key</span>=><span>$val</span>) { <span>echo</span><span>"auto["</span>.<span>$key</span>.<span>"]="</span>.<span>$val</span>.<span>"<br>"</span>; } <span>//输出</span><span>/* sort排序: auto[0]=bens1 auto[1]=bens15 auto[2]=bens2 auto[3]=bens31 auto[4]=bens41 natsort排序: auto[c]=bens1 auto[b]=bens2 auto[e]=bens15 auto[a]=bens31 auto[d]=bens41 */</span></span></span></code>
<code><span><span><?php</span><span>$auto1</span> = <span>array</span>(<span>"b"</span>=><span>"bens2"</span>,<span>"e"</span>=><span>"bens20"</span>,<span>"a"</span>=><span>"bens31"</span>,<span>"d"</span>=><span>"bens41"</span>,<span>"c"</span>=><span>"bens1"</span>,<span>"e"</span>=><span>"bens15"</span>); shuffle(<span>$auto1</span>); <span>foreach</span>(<span>$auto1</span><span>as</span><span>$key</span>=><span>$val</span>) { <span>echo</span><span>"auto["</span>.<span>$key</span>.<span>"]="</span>.<span>$val</span>.<span>"<br>"</span>; } <span>//输出</span><span>/* auto[0]=bens41 auto[1]=bens15 auto[2]=bens1 auto[3]=bens2 auto[4]=bens31 */</span></span></span></code>