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>