将多维数组数据按数字顺序排序 [英] Sort multidimensional array data into numerical order

查看:110
本文介绍了将多维数组数据按数字顺序排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码如下:

foreach ($final_array as $index => $data) {
    echo $data[1];
}

我想要做的是将$ data [1]按数字顺序排序.我已经尝试过asort()和natsort()之类的东西,但是没有任何效果.任何帮助将不胜感激.

What I want to do is sort $data[1] into numerical order. I've tried things like asort() and natsort(), but nothing worked. Any help would be HIGHLY appreciated.

这是我的数组的样子:

Array
(
    [1] => Array
    (
        [0] => Awesomedude123
        [1] => 399,408
        [2] => September 16, 2012
    )

    [2] => Array
    (
        [0] => Username11
        [1] => 1,914,144
        [2] => September 16, 2012
    )

    [3] => Array
    (
        [0] => EpicSurfer
        [1] => 1,031,427
        [2] => September 16, 2012
    )
)

推荐答案

您始终可以使用

You can always use usort for tricky array sorting:

function number_compare($a, $b)
{
    $t1 = str_replace( ',', '', $a[1] );
    $t2 = str_replace( ',', '', $b[1] );
    return $t1 - $t2;
}    
usort($array, 'number_compare');

这篇关于将多维数组数据按数字顺序排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆