按列值对 2dim 数组进行排序
我有一个数据结构,看起来
Array
(
[0] => Array
(
[0] => something
[1] => 1296986500
)
[1] => Array
(
[0] => something else
[1] => 1296600100
)
[2] => Array
(
[0] => another thing
[1] => 1296831265
)
)
我正在尝试根据整数(unix 时间戳)对数组进行排序。以下函数对我来说看起来不错,但没有按照我想要的方式排序。
function cmp($a, $b)
{
if ($a[1] == $b[1]) {
return 0;
}
return ($a[1] < $b[1]) ? -1 : 1;
}
注意 在类中调用此函数时,OO 语法如下
uasort($_data, array($this, 'cmp'));
I have a data structure that looks like
Array
(
[0] => Array
(
[0] => something
[1] => 1296986500
)
[1] => Array
(
[0] => something else
[1] => 1296600100
)
[2] => Array
(
[0] => another thing
[1] => 1296831265
)
)
I'm trying to sort the array based off of the integer which is a unix timestamp. The following function looks right to me but is not sorting the way I want.
function cmp($a, $b)
{
if ($a[1] == $b[1]) {
return 0;
}
return ($a[1] < $b[1]) ? -1 : 1;
}
NOTE
when calling this function within a class the OO syntax is the following
uasort($_data, array($this, 'cmp'));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这会按升序对您的时间戳进行排序;对于降序,翻转第二个比较(即将
$a[1] < $b[1]
更改为$a[1] > $b[1]
):That sorts your timestamps in ascending order; for descending order, flip the second comparison (i.e. change
$a[1] < $b[1]
to$a[1] > $b[1]
):您可以将时间戳设置为枢轴。并使用 array_multisort()。
You can setup time stamp as pivot. And use array_multisort().