CSV文件的多维数组排序过程 [英] Multidimensional array sorting procedure for csv files

查看:114
本文介绍了CSV文件的多维数组排序过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中有一个数组.像这样设置

There is an array, in PHP. It is setup like so

$array_var = array(array(1,2,3,4), array(5,6,7,8), array(3,5,3,9));

此数组来自使用fgetcsv函数获取的csv文件.如果我要正确地回显数组以显示其内容,我将使其显示为:

This array is from a csv file obtained using the fgetcsv function. If I was to echo out the array properly to display its contents I would make it so it shows like this:

field1  field2  field3  field4
  1       2       3       4
  5       6       7       8
  3       5       3       9

等等等.

现在我要对该数组进行排序.但是我只想对所有数组中的一列进行排序.换句话说,例如,我要获取主数组内每个数组中的每个第三个值,并按字母顺序升序列出.因此,对于特定情况,我们将从上表中获取field3中的每个值,然后对其进行排序.并使其排序的最终结果将重新排列各列,以便它们正确地与它们的值对齐.

Now I want to sort this array. But I want to sort only one of the columns in all of the arrays. Put another way, and for example, I want to take every third value in every array inside the main array and list them ascendingly, alphabetically. So for a specific case, we would take every value in field3, from the above table, and sort it. And also make it so that the end result of the sort will rearrange the columns so that they are correctly lined up with their values.

最终结果

field1  field2  field3  field4
  1       2       3       4
  3       5       3       9
  5       6       7       8

等等等.

这怎么完成?

挑战的原因是我正在尝试从csv文件中的单个列中删除重复项.我认为最快的方法是对值进行排序并查找范围内的匹配项.

The reason for the challenge is I am trying to remove duplicates from a single column in a csv file. I think the fastest way to do it is to sort the values and look for matches in range.

推荐答案

由于您在解释中遗漏了某些内容,因此很难给您确切的答案.例如,应如何在内部对按某一列排序但在其他列中具有差异的行进行排序?应该按其他列对它们进行排序,以原始顺序保留它们还是可以以任意顺序放置它们?

It's difficult to give you an exact answer since there are things you leave out from your explanation. For example, how should lines that are sorted by one of the columns but have differences in other columns be sorted internally? Should they be sorted by some other column, be left in the original order or could they be placed in an arbitrary order?

鉴于我对您问题的解释方式,我可能会定义自己的类进行比较.

Given the way I interpret your question, I would probably define my own class for comparisons.

<?php
class ColumnCompare {
  function __construct($column) {
    $this->column = $column;
  }

  function compare($a, $b) {
    if ($a[$this->column] == $b[$this->column]) {
      return 0;
    }
    return ($a[$this->column] < $b[$this->column]) ? -1 : 1;
  }
}

// Hard-coded input
$array_var = array(array(1,2,3,4), array(5,6,7,8), array(3,5,3,9));
$sort_by_col = 2;

// Create object for sorting by a particular column
$obj = new ColumnCompare($sort_by_col);
usort($array_var, array($obj, 'compare'));

// Write CSV to standard output
$sout = fopen('php://stdout', 'w');
foreach ($array_var as $fields) {
  fputcsv($sout, $fields);
}
fclose($sout);
?>

最后,您提出了另一个问题,这是无法回答的.如果您从单个列中删除重复项,那么该行的其余部分应该如何处理?应该只保留一行,在那种情况下应该保留哪一行?您也可以通过删除重复项"来表示要删除值并将NULL放在它们的位置.如果您希望解决该特定问题,则需要提供一些详细信息.

In the end you pose another question, which is impossible to answer. If you remove duplicates from a single column, what is then supposed to happen with the rest of the line? Should only one line be kept, and in that case which one? You could also by "remove duplicates" mean that you want to remove the values and put NULL in their positions. If you want that particular problem solved, you need to provide some details.

如果您的CSV文件非常简单,并且任何一行上重复的值都是相同的(在您的示例中,编辑之前就是这种情况),则可以轻松地运行诸如

If your CSV file is really simple, and all the values on any line with duplicates are identical (which was the case in your example before it was edited), you can easily run a command such as

sort myfile.csv | uniq

但是我觉得它比这更复杂. uniq还具有仅返回重复行的设置.也可以编写命令从每行中检索特定列并对其进行操作.但是就像我说的那样,没有更多信息就不可能构造这样的命令.

but I have a feeling that it's more complicated than that. uniq also has settings to return only the duplicate lines for example. It's also possible to write commands to retrieve a certain column from each line and operate on that. But like I said, it's not possible to construct such a command without more information.

这篇关于CSV文件的多维数组排序过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆