doudouji2016 2018-12-13 23:51
浏览 259

使用PHP对列进行CSV数据排序

I've looked on Stack Overflow with no solution that seems to work. Know that I'm a newbie!

Suppose I have a file (data.csv) with the following contents:

year,total
1990,57.58
2011,73.28
1880,54.67
1996,53.41
1950,53.22
1979,52.76
1876,52.62
1883,52.35
1882,52.12
2018,52.23

...then import the data with PHP:

$csv = array_map('str_getcsv', file('data.csv'));

How would I sort both the year and total by the total column in ascending order (such that 1882/52.12 are under $csv[0] in the array and 2011/73.28 are under $csv[10]?

The following and a print($csv); does not seem to be getting the items in the right order:

function compare($a, $b) {
    return ($b[0][1] - $a[0][1]);
}
usort($csv, "compare");

Do I need to use typecasting? Thank you!

  • 写回答

2条回答 默认 最新

  • dongmeng1402 2018-12-14 00:24
    关注

    This will do the trick:

    You have to modify the function like this:

    function compare($a, $b)
    {
        // here, comparing "total" column of each row:
        return $a[1] >= $b[1] ? 1 : -1;
    }
    

    And the new ordering will be:

                Array
                (
                    [0] => Array
                        (
                            [0] => 1882
                            [1] => 52.12
                        )
    
                    [1] => Array
                        (
                            [0] => 2018
                            [1] => 52.23
                        )
    
                    [2] => Array
                        (
                            [0] => 1883
                            [1] => 52.35
                        )
    
                    [3] => Array
                        (
                            [0] => 1876
                            [1] => 52.62
                        )
    
                    [4] => Array
                        (
                            [0] => 1979
                            [1] => 52.76
                        )
    
                    [5] => Array
                        (
                            [0] => 1950
                            [1] => 53.22
                        )
    
                    [6] => Array
                        (
                            [0] => 1996
                            [1] => 53.41
                        )
    
                    [7] => Array
                        (
                            [0] => 1880
                            [1] => 54.67
                        )
    
                    [8] => Array
                        (
                            [0] => 1990
                            [1] => 57.58
                        )
    
                    [9] => Array
                        (
                            [0] => 2011
                            [1] => 73.28
                        )
    
                )
    
    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示