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.9 Ein Array mit Kommata ausgeben

Problem

Sie möchten ein Array so ausgeben, dass die Elemente durch Kommata getrennt sind, wenn es mehr als zwei Elemente sind, vor dem letzten Element aber ein »und« steht.

Lösung

Verwenden Sie die in Listing 4.1 dargestellte Funktion pc_array_to_comma_string(), die einen korrekten String liefert.

function pc_array_to_comma_string($array) {

    switch (count($array)) {
    case 0:
        return '';

    case 1:
        return reset($array);

    case 2:
        return join(' und ', $array);

    default:
        $last = array_pop($array);
        return join(', ', $array) . " und $last";
    }
}

Listing 4.1 pc_array_to_comma_string()

Diskussion

Wenn Sie eine Liste von Dingen ausgeben wollen, sollte dies in einer grammatikalisch korrekten Form geschehen. Es sieht unschön aus, wenn ...

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