Rumah >pembangunan bahagian belakang >tutorial php >解读PHP数组排序
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>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });
以上就介绍了 解读PHP数组排序,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。