Reputation:
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));
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
etc etc..
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.
End result
field1 field2 field3 field4
1 2 3 4
3 5 3 9
5 6 7 8
etc etc..
How can this be accomplished?
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.
Upvotes: 0
Views: 1827
Reputation: 3482
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);
?>
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.
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
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.
Upvotes: 2