Home >Backend Development >PHP Tutorial >array_multisort — Sort multiple arrays or multidimensional arrays
array_multisort — Sort multiple arrays or multidimensional arrays. Returns true on success, false otherwise.
The input array is treated as a table column and sorted by rows. This is similar to the function of SQL's ORDER BY clause. The first array is the main array to be sorted. If the rows (values) in the array are compared to be the same, they are sorted according to the size of the corresponding value in the next input array, and so on. ?
The first parameter must be an array. Each of the following arguments can be an array or a sort flag listed below.
Sort order flags:
? SORT_ASC – Sort in ascending order
SORT_DESC – Sort in descending order
? Sort type flags:
SORT_REGULAR – Compare items in the usual way
SORT_NUMERIC – Sort items in numerical order Compare
?SORT_STRING - Compare items according to strings
SORT_STRING and SORT_REGULAR are case-sensitive, and uppercase letters will be sorted before lowercase letters. ?
?Two similar sorting flags cannot be specified after each array. The sort flags specified after each array are valid only for that array – before that the default values SORT_ASC and SORT_REGULAR were used.
//String key names will be retained, but numeric keys will be re-indexed, starting from 0 and increasing by 1.
Sorting a two-dimensional array requires the ID to be in descending order and the name to be in ascending order?
<code><span>$arrData</span> = [ [<span>'id'</span> => <span>1</span>, <span>'name'</span> => <span>'haha'</span>], [<span>'id'</span> => <span>0</span>, <span>'name'</span> => <span>'aaha'</span>], [<span>'id'</span> => <span>2</span>, <span>'name'</span> => <span>'caha'</span>], [<span>'id'</span> => <span>4</span>, <span>'name'</span> => <span>'zaha'</span>], [<span>'id'</span> => <span>16</span>, <span>'name'</span> => <span>'daha'</span>], ]; <span>/* 现在有了包含有行的数组,但是 array_multisort() 需要一个包含列的数组,因此用以下代码来取得列,然后排序。 ? */</span><span>foreach</span>(<span>$arrData</span><span>as</span><span>$key</span>=><span>$value</span>) {? <span>$id</span>[<span>$key</span>]=<span>$value</span>[<span>'id'</span>]; <span>$arr</span>[<span>$key</span>]=<span>$value</span>[<span>'name'</span>]; } <span>// 把排序数组 $arrData 作为最后一个参数</span>?array_multisort(<span>$id</span>, SORT_DESC, <span>$arr</span>, SORT_ASC, <span>$arrData</span>); print_r(<span>$arrData</span>);</code>
Please indicate the address for reprinting or sharing. Thank you: http://blog.csdn.net/w19981220
Copyright Statement: Respecting the fruits of others’ labor is respecting Thank myself! !
The above introduces array_multisort - sorting multiple arrays or multi-dimensional arrays, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.