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

使用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 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算