0

Raw output

This is the output I get. I want it like that, just that all rows should be sorted by first-/lastname. How can I do that?

 function cmp($a, $b){
    return @($a[1] < $b[1]) ? 1 : -1;
}

function print_raw($raw, $first){
foreach($raw as $val){
    echo '<tr>';
    $value = explode(",", $val);

    //usort($value, 'cmp');
...
}

Somehow, my function doesn't work properly, so my output looks like this afterwards:

Output after function call

How do I have to write my function, so the order of entries inside my array stays the same while it is sorted by it's first-/lastname ?

EDIT

$fp = file($dateiname) or die("Fehler beim Öffnen der Datei!");
print_raw($fp, $first);
Stöger
  • 155
  • 1
  • 11
  • 1
    You want to keep the array the same, but you want to change it? Could you explain what you mean? – James Lalor Mar 07 '16 at 14:42
  • Where does the data come from? Maybe there (e.g. MySQL) you have some sorting functions? – Jan Mar 07 '16 at 14:43
  • I want that the entries in one row stay the same, but the whole row should be sorted by the first name of each row. Do you understand what I mean? Like in my first picture, the rows should be like: Diane comes before Virginia for e.g. – Stöger Mar 07 '16 at 14:43
  • Well, you're sorting each row, when you should be sorting the whole array instead. First process `$raw` into a proper multi-dimensional array, then sort it. – deceze Mar 07 '16 at 14:44
  • @Jan I'm reading the data out of a .csv file. I'll edit my question and put the rest in so you know where the data comes from. – Stöger Mar 07 '16 at 14:44
  • Do you mean you want to keep the columns the same, but sort the rows? – James Lalor Mar 07 '16 at 14:45
  • Take a look at: http://stackoverflow.com/questions/2699086/sort-multi-dimensional-array-by-value – James Lalor Mar 07 '16 at 14:50
  • You're sorting each column by content. Is that right? Don't you need to sort rows by a specific column? – apokryfos Mar 07 '16 at 14:55
  • Yes, that would be exactly what I need @apokryfos – Stöger Mar 07 '16 at 15:03
  • Use `fgetcsv` to read all lines of your file into an array. Then sort that array. Don't bother with `file()` or manually `explode`ing each line. – deceze Mar 07 '16 at 15:32

0 Answers0