我将如何在 php 中按其值进行排序和数组


How would I sort and array by its values in php

我有一个名为$csv的数组,它包含具有年份,品牌和型号的汽车。

function readCSV($csvFile){
$file_handle = fopen($csvFile, 'r');
while (!feof($file_handle) ) {
    $line_of_text[] = fgetcsv($file_handle, 1024);
}
fclose($file_handle);
return $line_of_text;
}

// Set path to CSV file
$csvFile = 'csv/file.csv';
$csv = readCSV($csvFile);
array_shift($csv); 
foreach($csv as $car){
$year = $car[3];
$make = $car[4];
$model = $car[5];
echo $year;
}

这给了我——20112009201220122013

如何按最新到最旧顺序筛选要显示的结果?

$years = [];
foreach($csv as $car){
    $years[] = $car[3];
}
rsort($years);
foreach($years as $year) {
    echo $year;
}

如果需要从低到高的排序,请使用排序而不是 rsort。