Array sortieren

  • Array sortieren

    Ich versuche jetzt seit einer ganzen Weile meine Array zu sortieren.

    Also ich möchte, dass meine Arrays sortieren. Das sollte mit rsort() eigentlich funktionieren. Aber ich bekomme das irgenwie nicht hin. Das Script soll mir die Array-Zeile, welche die höchsten Votes hat als erstes anzeigen.

    Mein php-Code:

    Quellcode

    1. <table width="100%" border="0" cellspacing="0" cellpadding="0">
    2. <tr>
    3. <th width="25" bgcolor="#408040" scope="col"><div align="center"></div></th>
    4. <th bgcolor="#99CC99" scope="col"><div align="left">&nbsp;Artist / Titel </div></th>
    5. <th width="100" bgcolor="#99CC99" scope="col"><div align="left">Votes</div></th>
    6. <th width="25" bgcolor="#99CC99" scope="col"><div align="center"></div></th>
    7. </tr>
    8. <?php
    9. $file_chart = "data/chart.mtd";
    10. $file_read = fopen("$file_chart","r");
    11. $line = 0;
    12. // Auslesen der Datei
    13. while (!feof($file_read))
    14. {
    15. $chart1 = fgets($file_read,64);
    16. list( $artist [] , $song [] , $votes []) = explode ( "," , $chart1 );?>
    17. <tr>
    18. <th width="25" bgcolor="#99CC99"><div align="center"><? echo $line++; ?></div></th>
    19. <td><div align="left">&nbsp;<? echo $artist[1]; ?><br>&nbsp;<? echo $song[1]; ?></div></td>
    20. <td width="100"><div align="left"><? echo $votes[1]; ?></div></td>
    21. <td width="25"><div align="center"></div></td>
    22. </tr>
    23. <?
    24. }
    25. fclose($file_read);
    26. ?>
    27. </table>
    Alles anzeigen



    Meine Datentabelle ist wie folgt aufgebaut(eine Zeile);
    TipTop,TipTop,111
  • Ganz einfach:
    Dein Array -> $array = array_reverse(natsort($array));

    Der Sortiert dir das Array in einer natürlichen Reihenfolge (bei Zahlen ganz wichtig) und kert es dann mit array_reverse um, sodass die höchste Zahl als oberstes Steht...

    Ich denke, damit kannst du weiterarbeiten =)
    Du schaffst das schon in dein Script umzusetzen...
    Wenn nicht, wir sind 24h/7Tage erreichbar =)


    [1] http://de.php.net/manual/de/function.natsort.php
    [2] http://de.php.net/manual/de/function.array-reverse.php
  • Array sortieren

    Also ich hab das jetzt mal so gemacht. Für jeden in der Datei enthaltenen Eintrag soll er mit eine Zeile in einer Tabelle ausgeben. Das funktioniert auch wunderbar. Aber die Sortierung funktioniert nicht so . Es soll nach $votes [] abwärts sortiert werden. Mit der Funktion rsort komme ich nicht klar. Bzw. ich weiß nicht wo ich diese einsetzen soll.


    Quellcode

    1. <?
    2. $file_chart = "data/chart.mtd";
    3. $file_read = fopen("$file_chart","r");
    4. $line = 0;
    5. $a_artist = 0;
    6. $a_song = 0;
    7. $a_votes = 0;
    8. // Auslesen der Datei
    9. while (!feof($file_read))
    10. {
    11. $chart1 = fgets($file_read,64);
    12. list( $artist [] , $song [] , $votes []) = explode ( "," , $chart1 );?>
    13. <tr>
    14. <th width="25"><? echo $line; ?></th>
    15. <td><? echo $artist[$a_artist]; ?>/<? echo $song[$a_song]; ?></td>
    16. <td width="80"><? echo $votes[$a_votes]; ?></td>
    17. </tr>
    18. <?
    19. }
    20. fclose($file_read);
    21. ?>
    Alles anzeigen
  • So ich habe das jetzt mal so gelöst. Funktioniert super. Auch wenn es vielleicht etwas umständlich ist.

    Quellcode

    1. <?php
    2. define('ARTIST',0);
    3. define('SONG',1);
    4. define('VOTES',2);
    5. function cmp($a, $b)
    6. {
    7. if ($a[VOTES] == $b[VOTES]) {
    8. return 0;
    9. }
    10. return (intval($a[VOTES]) > intval($b[VOTES])) ? -1 : 1;
    11. }
    12. $file_chart = "user.txt";
    13. $file_read = fopen("$file_chart","r");
    14. $eintraege=array();
    15. while (!feof($file_read))
    16. {
    17. $chart1 = fgets($file_read,64);
    18. $eintraege[] = explode ( "," , $chart1 );
    19. }
    20. usort($eintraege,'cmp');
    21. $line=1;
    22. foreach($eintraege as $eintrag) {?>
    23. <tr>
    24. <th width="25"><?php echo $line; ?></th>
    25. <td><?php echo $eintrag[ARTIST]; ?>/<?php echo $eintrag[SONG]; ?></td>
    26. <td width="80"><?php echo $eintrag[VOTES]; ?></td>
    27. </tr>
    28. <?php
    29. $line++;
    30. }
    31. fclose($file_read);
    32. ?>
    Alles anzeigen
  • den Beitrag hab ich irgendwie ganz übersehen.
    Aber wenn du das Problem selbst lösen konntest: um so besser!

    [phpdoc]usort[/phpdoc] ist ne schicke Lösung. Keine Einwände.
    Was mir an deinem Code jedoch nicht so gefällt ist, dass du deinen PHP Code so oft unterbrichst

    So eine zusammenhängende Schleife mit echo ist doch viel schöner:

    Quellcode

    1. foreach($eintraege as $eintrag) {
    2. echo '<tr>
    3. <th width="25">'. $line .'</th>
    4. <td>'. $eintrag[ARTIST] .'/'. $eintrag[SONG] .'</td>
    5. <td width="80">'. $eintrag[VOTES] .'</td>
    6. </tr>';
    7. $line++;
    8. }