php经过长时间的发展,很多用户都很了解它了,上网收集了一些关于php二维数组的相关知识,这里和大家一起分享一下,php本身是有一个多维数组排序的函数的。
<OL class=dp-xml><LI class=alt><SPAN><SPAN>boolarray_multisort(array$ar1[,mixed$arg[,mixed$...[,array$...]]]) </SPAN></SPAN></LI></OL>
下面是手册当中对于array_multisort函数的描述:
<OL class=dp-xml><LI class=alt><SPAN><SPAN>array('id'=</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></FONT></STRONG></SPAN><SPAN>array(1,3,2), </SPAN></SPAN><LI class=""><SPAN>'data'=</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></FONT></STRONG></SPAN><SPAN>array('a','c','b')) </SPAN></SPAN></LI></OL>只要按照id进行多维排序,就可以了。但是很多时候,我们构造出的PHP二维数组是这样的:
<OL class=dp-xml><LI class=alt><SPAN><SPAN>array( </SPAN></SPAN><LI class=""><SPAN>array('id'=</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></FONT></STRONG></SPAN><SPAN>1,'data'=</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></FONT></STRONG></SPAN><SPAN>'a'), </SPAN></SPAN><LI class=alt><SPAN>array('id'=</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></FONT></STRONG></SPAN><SPAN>3,'data'=</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></FONT></STRONG></SPAN><SPAN>'c'), </SPAN></SPAN><LI class=""><SPAN>array('id'=</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></FONT></STRONG></SPAN><SPAN>2,'data'=</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></FONT></STRONG></SPAN><SPAN>'b') </SPAN></SPAN><LI class=alt><SPAN>); </SPAN></LI></OL>数组的元素是按行排列的,需要按其中的一列进行排序。PHP好像也没提供类似矩阵转置的函数,所以不能直接使用array_multisort进行多维排序。不过只需要先把排序的列抽取出来,作为第一个参数传给array_multisort即可。
立即学习“PHP免费学习笔记(深入)”;
<OL class=dp-xml><LI class=alt><SPAN><SPAN>functionmulti_array_sort($multi_array,$sort_key,$</SPAN><SPAN class=attribute><FONT color=#ff0000>sort</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>SORT_ASC</FONT></SPAN><SPAN>){ </SPAN></SPAN><LI class=""><SPAN>if(is_array($multi_array)){ </SPAN><LI class=alt><SPAN>foreach($multi_arrayas$row_array){ </SPAN><LI class=""><SPAN>if(is_array($row_array)){ </SPAN><LI class=alt><SPAN>$key_array[]=$row_array[$sort_key]; </SPAN><LI class=""><SPAN>}else{ </SPAN><LI class=alt><SPAN>return-1; </SPAN><LI class=""><SPAN>} </SPAN><LI class=alt><SPAN>} </SPAN><LI class=""><SPAN>}else{ </SPAN><LI class=alt><SPAN>return-1; </SPAN><LI class=""><SPAN>} </SPAN><LI class=alt><SPAN>array_multisort($key_array,$sort,$multi_array); </SPAN><LI class=""><SPAN>return$multi_array; </SPAN><LI class=alt><SPAN>} </SPAN></LI></OL>以上就是简单的PHP二维数组的介绍,希望对大家有帮助。
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号