Skip to Main Content
PHP 5 Kochbuch, Third Edition
book

PHP 5 Kochbuch, Third Edition

by David Sklar, Adam Trachtenberg, Carsten Lucke, Matthias Brusdeylins, Ulrich Speidel, Stephan Schmidt
September 2009
Intermediate to advanced content levelIntermediate to advanced
912 pages
48h 11m
German
O'Reilly Verlag
Content preview from PHP 5 Kochbuch, Third Edition

4.17 Ein Array über ein berechnetes Feld sortieren

Problem

Sie möchten eine eigene Sortierroutine definieren.

Lösung

Verwenden Sie usort() in Kombination mit einer benutzerdefinierten Vergleichsfunktion:

// In umgekehrte natürliche Anordnung sortieren.
function natrsort($a, $b) {
    return strnatcmp($b, $a);
}

$tests = array('test1.php', 'test10.php', 'test11.php', 'test2.php');
usort($tests, 'natrsort');

Diskussion

Die Vergleichsfunktion muss einen Wert größer als 0 liefern, wenn $a > $b, 0, wenn $a == $b, und einen Wert kleiner als 0, wenn $a < $b ist. Bei einer umgekehrten Sortierung tut sie das Gegenteil. Die Funktion strnatcmp() in der Lösung folgt diesen Regeln.

Um die Sortierung umzukehren, multiplizieren Sie die Rückgabewerte von strnatcmp($a, $b) ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PHP programmieren unter Windows

PHP programmieren unter Windows

Arno Hollosi
CSS Kochbuch, 3rd Edition

CSS Kochbuch, 3rd Edition

Joergen Lang, Christopher Schmitt

Publisher Resources

ISBN: 9783868993271Purchase book