Home >Backend Development >PHP Tutorial >Experts talk about PHP two-dimensional arrays in detail_PHP tutorial
After a long period of development of PHP, many users are familiar with it. I have collected some relevant knowledge about PHP two-dimensional arrays on the Internet. I will share it with you here. PHP itself has a multi-dimensional array sorting Functional.
<ol class="dp-xml"><li class="alt"><span><span>boolarray_multisort(array$ar1[,mixed$arg[,mixed$...[,array$...]]]) </span></span></li></ol>
The following is the description of the array_multisort function in the manual:
<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> <li class=""> <span>'data'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>array('a','c','b')) </span> </li> </ol>
Just do multidimensional sorting by id and you're good to go. But many times, the PHP two-dimensional array we construct looks like this:
<ol class="dp-xml"> <li class="alt"><span><span>array( </span></span></li> <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> </li> <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> </li> <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> </li> <li class="alt"><span>); </span></li> </ol>
The elements of the array are arranged in rows and need to be sorted by one of the columns. PHP does not seem to provide a function similar to matrix transposition, so array_multisort cannot be used directly for multidimensional sorting. But you only need to extract the sorted column first and pass it to array_multisort as the first parameter.
<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> <li class=""><span>if(is_array($multi_array)){ </span></li> <li class="alt"><span>foreach($multi_arrayas$row_array){ </span></li> <li class=""><span>if(is_array($row_array)){ </span></li> <li class="alt"><span>$key_array[]=$row_array[$sort_key]; </span></li> <li class=""><span>}else{ </span></li> <li class="alt"><span>return-1; </span></li> <li class=""><span>} </span></li> <li class="alt"><span>} </span></li> <li class=""><span>}else{ </span></li> <li class="alt"><span>return-1; </span></li> <li class=""><span>} </span></li> <li class="alt"><span>array_multisort($key_array,$sort,$multi_array); </span></li> <li class=""><span>return$multi_array; </span></li> <li class="alt"><span>} </span></li> </ol>
The above is a simple introduction to two-dimensional arrays in PHP. I hope it will be helpful to everyone.