使用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!

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
                    )

            )

Three problems with your comparison function.

  1. The items that will be compared in your comparison function are going to be arrays corresponding to rows from your CSV file. For example, $a and $b will be things like [1990,57.58] and [1950,53.22].

    When you refer to those items in your comparison function, you're looking at index [0][1], but that doesn't exist; the arrays don't have that second dimension. $a[0][1] and $b[0][1] will both be null, so no sorting happens. (You might think you'd get some kind of warning or notice for trying to refer to an int or float with an array index, but you won't, just one of the weird things about PHP.)

  2. You want to sort in ascending order, but putting $b first will sort in descending order.

  3. The comparison function should return an int greater than, less than, or equal to zero depending on the result of the comparison, but yours will return a float. There are various way to make it return an int. The other answer shows how to do it with a ternary expression. If you're using PHP 7, you can use the combined comparison operator, like this:

    function compare($a, $b) {
        return $a[1] <=> $b[1];
    }
    

Also, you don't need to define a named comparison function, you can use an anonymous function for the second argument if you like.

usort($csv, function($a, $b) {
    return $a[1] <=> $b[1];
});