foreach funktion bei arrays

  • Naja.. um halt mit dem gesamten Array eine Aktion auszühren, z.B. daraus eine Ausgabe erzeugen.
    Nehmen wir an du lädst dir alle Kommentare eines Bloges in ein Array, wie willst du die ansonsten bequem ausgeben? Du könntest mit next() arbeiten, aber das ist auch nicht unbedingt fein..
    Aber im Allgemeinen ist das ein sehr häufig benutztes Sprachelement :)

  • Nimm z. B. eine MySQL-Insert-Funktion, die ein Array mit den einzufügenden Daten entgegennimmt:

    PHP
    1. <span class="syntaxdefault"></span><span class="syntaxkeyword">function </span><span class="syntaxdefault">insert</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$eintrag</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">$table</span><span class="syntaxkeyword">){<br /> if (!</span><span class="syntaxdefault">is_array</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$eintrag</span><span class="syntaxkeyword">)){<br /> echo </span><span class="syntaxstring">"Falscher Parametertyp"</span><span class="syntaxkeyword">; return </span><span class="syntaxdefault">false</span><span class="syntaxkeyword">;<br /> }<br /> foreach (</span><span class="syntaxdefault">$eintrag </span><span class="syntaxkeyword">as </span><span class="syntaxdefault">$index </span><span class="syntaxkeyword">=> </span><span class="syntaxdefault">$value</span><span class="syntaxkeyword">){<br /> </span><span class="syntaxdefault">$index_escaped </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">mysql_real_escape_string</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">trim</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$index</span><span class="syntaxkeyword">));<br /> </span><span class="syntaxdefault">$value_escaped </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">mysql_real_escape_string</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">trim</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$value</span><span class="syntaxkeyword">));<br /> </span><span class="syntaxdefault">$eintrag_escaped</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">$index_escaped</span><span class="syntaxkeyword">] = </span><span class="syntaxdefault">$value_escaped</span><span class="syntaxkeyword">;<br /> }<br /> </span><span class="syntaxdefault">$table_escaped </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">mysql_real_escape_string</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">trim</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$table</span><span class="syntaxkeyword">)); <br /> </span><span class="syntaxdefault">$sql </span><span class="syntaxkeyword">= </span><span class="syntaxstring">"INSERT INTO `$table_escaped` ("</span><span class="syntaxkeyword">;<br /> </span><span class="syntaxdefault">$i </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">0</span><span class="syntaxkeyword">;<br /> foreach (</span><span class="syntaxdefault">$eintrag_escaped </span><span class="syntaxkeyword">as </span><span class="syntaxdefault">$spalte </span><span class="syntaxkeyword">=> </span><span class="syntaxdefault">$inhalt</span><span class="syntaxkeyword">) {<br /> if (</span><span class="syntaxdefault">$i</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">$sql </span><span class="syntaxkeyword">.= </span><span class="syntaxstring">","</span><span class="syntaxkeyword">;<br /> </span><span class="syntaxdefault">$sql </span><span class="syntaxkeyword">.= </span><span class="syntaxstring">"`$spalte`"</span><span class="syntaxkeyword">;<br /> </span><span class="syntaxdefault">$i</span><span class="syntaxkeyword">++;<br /> }<br /> </span><span class="syntaxdefault">$sql </span><span class="syntaxkeyword">.= </span><span class="syntaxstring">") VALUES ("</span><span class="syntaxkeyword">;<br /> </span><span class="syntaxdefault">$i </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">0</span><span class="syntaxkeyword">;<br /> foreach (</span><span class="syntaxdefault">$eintrag_escaped </span><span class="syntaxkeyword">as </span><span class="syntaxdefault">$spalte </span><span class="syntaxkeyword">=> </span><span class="syntaxdefault">$inhalt</span><span class="syntaxkeyword">) {<br /> if (</span><span class="syntaxdefault">$i</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">$sql </span><span class="syntaxkeyword">.= </span><span class="syntaxstring">","</span><span class="syntaxkeyword">;<br /> </span><span class="syntaxdefault">$sql </span><span class="syntaxkeyword">.= </span><span class="syntaxstring">"'$inhalt'"</span><span class="syntaxkeyword">;<br /> </span><span class="syntaxdefault">$i</span><span class="syntaxkeyword">++;<br /> }<br /> </span><span class="syntaxdefault">$sql </span><span class="syntaxkeyword">.= </span><span class="syntaxstring">");"</span><span class="syntaxkeyword">;<br /> return </span><span class="syntaxdefault">$sql</span><span class="syntaxkeyword">;<br /> } </span><span class="syntaxdefault"></span>