排序PHP中的多维数组? [英] Sorting a multidimensional array in PHP?
问题描述
我有这样的一个数组
阵列(5){
[0] =>
阵列(5){
[0] =>
字符串(7)jannala
[1] =>
串(10)2009-11-16
[2] =>
串(29)
&所述p为H.; Jotain mukavaa&下; / P>
[3] =>
INT(12)
[4] =>
INT(1270929600)
}
[1] =>
阵列(5){
[0] =>
字符串(7)jannala
[1] =>
串(10)2009-11-16
[2] =>
串(51)
< P>SaapumiseräII / 09 astuu palvelukseen< / P>
[3] =>
INT(11)
[4] =>
INT(1270929600)
}
...
}
我需要做的是基于磁盘阵列的[X] [4](Unix时间戳值)数组进行排序。我将如何实现这一目标?
使用比较功能,在这种情况下,阵列的Unix时间戳值进行比较:
功能比较($ X,$ Y){
如果($ X [4] == $ y的[4])
返回0;
否则如果($ X [4]所述; $ y的[4])
返回-1;
其他
返回1;
}
,然后使用 usort <叫它/ code>
这样功能:
usort($ nameOfArray,比较);
此功能将整理使用用户提供的比较函数的值的数组。如果你想需要排序的数组由一些非平凡的标准进行排序,则应该使用此功能。
块引用>PHP措施:usort 手册。
I have this kind of an array
array(5) { [0]=> array(5) { [0]=> string(7) "jannala" [1]=> string(10) "2009-11-16" [2]=> string(29) " <p>Jotain mukavaa.</p> " [3]=> int(12) [4]=> int(1270929600) } [1]=> array(5) { [0]=> string(7) "jannala" [1]=> string(10) "2009-11-16" [2]=> string(51) " <p>Saapumiserä II/09 astuu palvelukseen</p> " [3]=> int(11) [4]=> int(1270929600) } ... }
What I need to be done is to sort the array based on array's [x][4] (the unix timestamp value). How would I achieve this?
解决方案use a compare function, in this case it compares the array's unix timestamp value:
function compare($x, $y) { if ( $x[4] == $y[4] ) return 0; else if ( $x[4] < $y[4] ) return -1; else return 1; }
and then call it with using the
usort
function like this:usort($nameOfArray, 'compare');
This function will sort an array by its values using a user-supplied comparison function. If the array you wish to sort needs to be sorted by some non-trivial criteria, you should use this function.
Taken from PHP: usort manual.
这篇关于排序PHP中的多维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!