Mailinglisten-Archive |
Am 15.03.2005 um 13:49 schrieb Michael Borchers:
> <?php
> $typ = $_POST['typ'];
> $stck = $_POST['stck'];
>
> for ($t=0;$t<count($typ);$t++)
> {
> if ($stck[$t] != "0")
> {
> $typ_array[] = $stck[$t] . "x " . $typ[$t];
> }
> }
>
> $typ_array_count = count($typ_array);
>
> for($akf=0;$akf<$typ_array_count;$akf++)
> {
> $text = $text . $typ_array[$akf] . "<br>\n";
> }
>
> echo "Text: $text";
> ?>
Warum zwei schleifen?
Warum for schleifen? Durch `foreach()` kannst Du dir count() sparen.
Wenn Du das Array $typ_array nach der Ausgabe von $text nicht mehr
benötigt ist es völlig überflüssig.
$text = "";
foreach($typ AS $k => $v) {
if((int)$stck[$k] !== 0) {
$text .= $stck[$k] ." x ". $v . "<br>\n";
/* $typ_array[$k] = $stck[$k] ." x ". $v; */
}
}
echo "Text: ". $text;
Gruß
Aron
PS. count() in einer schleife aufzurufen ist ungünstig da es bei jedem
durchlaufen neu aufgerufen wird.
php::bar PHP Wiki - Listenarchive