如何在PHP中按特定列对CSV表数据进行排序?

如何在PHP中按特定列对CSV表数据进行排序?,php,sorting,csv,Php,Sorting,Csv,以下是CSV表: ------------------------- | Name | Age | Favorite | ------------------------- | John | 30 | Apple | ------------------------- | Bill | 25 | Grape | ------------------------- | Ann | 40 | Orange | ------------------------- 现在,严格使

以下是CSV表:

-------------------------
| Name | Age | Favorite |
-------------------------
| John | 30  | Apple    |
-------------------------
| Bill | 25  | Grape    |
-------------------------
| Ann  | 40  | Orange   |
-------------------------
现在,严格使用PHP,是否可以按照“年龄”的升序只对“收藏夹”进行排序?预期输出如下所示:

25 Grape
30 Apple
40 Orange

我一直在使用
fgetcsv
将它们回显到文档中,但它们当然不是按年龄升序排序的。是否可以将这些文件放入数组或其他东西中,按年龄排序,然后进行回显?

打开您的CSV文件:

function readCSV($file)
{
  $row      = 0;
  $csvArray = array();
  if( ( $handle = fopen($file, "r") ) !== FALSE ) {
    while( ( $data = fgetcsv($handle, 0, ";") ) !== FALSE ) {
      $num = count($data);
      for( $c = 0; $c < $num; $c++ ) {
        $csvArray[$row][] = $data[$c];
      }
      $row++;
    }
  }
  if( !empty( $csvArray ) ) {
    return array_splice($csvArray, 1); //cut off the first row (names of the fields)
  } else {
    return false;
  }
}

$csvData = readCSV($csvPath); //This is your array with the data
函数readCSV($file)
{
$row=0;
$csvArray=array();
if(($handle=fopen($file,“r”))!==FALSE){
while(($data=fgetcsv($handle,0,“;”)!==FALSE){
$num=计数($data);
对于($c=0;$c<$num;$c++){
$csvArray[$row][]=$data[$c];
}
$row++;
}
}
如果(!empty($csvArray)){
return array_splice($csvArray,1);//切掉第一行(字段名称)
}否则{
返回false;
}
}
$csvData=readCSV($csvPath)//这是包含数据的数组
然后,您可以使用它对值进行排序

<?php
// Obtain a list of columns
foreach ($csvData as $key => $row) {
    $age[$key]  = $row['volume'];
    $favorite[$key] = $row['edition'];
}

// Sort the data with age first, then favorite
// Add $csvData as the last parameter, to sort by the common key
array_multisort($age, SORT_ASC, $favorite, SORT_ASC, $csvData);
?>

打开您的CSV文件:

function readCSV($file)
{
  $row      = 0;
  $csvArray = array();
  if( ( $handle = fopen($file, "r") ) !== FALSE ) {
    while( ( $data = fgetcsv($handle, 0, ";") ) !== FALSE ) {
      $num = count($data);
      for( $c = 0; $c < $num; $c++ ) {
        $csvArray[$row][] = $data[$c];
      }
      $row++;
    }
  }
  if( !empty( $csvArray ) ) {
    return array_splice($csvArray, 1); //cut off the first row (names of the fields)
  } else {
    return false;
  }
}

$csvData = readCSV($csvPath); //This is your array with the data
函数readCSV($file)
{
$row=0;
$csvArray=array();
if(($handle=fopen($file,“r”))!==FALSE){
while(($data=fgetcsv($handle,0,“;”)!==FALSE){
$num=计数($data);
对于($c=0;$c<$num;$c++){
$csvArray[$row][]=$data[$c];
}
$row++;
}
}
如果(!empty($csvArray)){
return array_splice($csvArray,1);//切掉第一行(字段名称)
}否则{
返回false;
}
}
$csvData=readCSV($csvPath)//这是包含数据的数组
然后,您可以使用它对值进行排序

<?php
// Obtain a list of columns
foreach ($csvData as $key => $row) {
    $age[$key]  = $row['volume'];
    $favorite[$key] = $row['edition'];
}

// Sort the data with age first, then favorite
// Add $csvData as the last parameter, to sort by the common key
array_multisort($age, SORT_ASC, $favorite, SORT_ASC, $csvData);
?>


@PaulCrovella没有,因为他特别询问了CSV到数组的转换well@PaulCrovella不,因为他特别询问了CSV到数组的转换,你也可以这样阅读文件:$CSV=array\u map('str\u getcsv',file('data.CSV')@米歇尔:你没有错,但是数组映射比foreach慢得多。不仅仅是一点点,而是很多。通过替换这些函数(原生php数组函数非常慢),我成功地将脚本时间从3分钟缩短到3秒。有关更多信息,请参阅非常有趣的阅读,谢谢。我注意到,由于PHP7 array_-map实际上比任何东西都快,所以也可以这样读取文件:$csv=array_-map('str_-getcsv',file('data.csv')@米歇尔:你没有错,但是数组映射比foreach慢得多。不仅仅是一点点,而是很多。通过替换这些函数(原生php数组函数非常慢),我成功地将脚本时间从3分钟缩短到3秒。有关更多信息,请参阅非常有趣的阅读,谢谢。我注意到,由于PHP7数组_-map实际上速度几乎为零。