Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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?</p> <p>Given the way I interpret your question, I would probably define my own class for comparisons.</p> <pre><code>&lt;?php class ColumnCompare { function __construct($column) { $this-&gt;column = $column; } function compare($a, $b) { if ($a[$this-&gt;column] == $b[$this-&gt;column]) { return 0; } return ($a[$this-&gt;column] &lt; $b[$this-&gt;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); ?&gt; </code></pre> <p>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.</p> <p>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</p> <pre><code>sort myfile.csv | uniq </code></pre> <p>but I have a feeling that it's more complicated than that. <code>uniq</code> 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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload