1

I have a csv file (test.csv) like this (no header)

name2,age23,city5
name5,age55,city3
name3,age36,city4
name1,age18,city2
name4,age44,city1

I want to sort on name column like this ascending or descending order
name1,age18,city2
name2,age23,city5
name3,age36,city4
name4,age44,city1
name5,age55,city3

<?php
if (($handle = fopen("test.csv", "r")) !== FALSE) {
    $i=1;
    $row=0;
    $csv_row = array();

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $csv_row = $data;
?>
          <tr>
            <td><?php echo $i;?> </td>
            <td><?php echo $csv_row[1];?></td>
            <td><?php echo $csv_row[2];?></td>
            <td><?php echo $csv_row[3];?></td>
          </tr>
        <?php $i++;
}
 fclose($handle);
}
?>

How to sort data ($csv_row) on a column (Name) before print? Suggested answer is not working for me.

I am new in php. Many suggestion available but they are not working properly . Please give a simple solution. Thanks in advance.

N. Josh
  • 33
  • 4
  • Have you tried anything yet? Where exactly are you stuck? – Mureinik Jun 29 '19 at 09:45
  • 2
    Possible duplicate of [How to sort CSV table data in PHP by a specific column?](https://stackoverflow.com/questions/39421575/how-to-sort-csv-table-data-in-php-by-a-specific-column) – abhay Jun 29 '19 at 09:47
  • Please, can anybody feed the code of this answer (How to sort CSV table data in PHP by a specific column?) in my code ? – N. Josh Jul 01 '19 at 10:12

0 Answers0