Re: foreach() or for()

From: Date: Tue, 25 Mar 2003 07:59:54 +0000
Subject: Re: foreach() or for()
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-140923@lists.php.net to get a copy of this message
Hi, Tuesday, March 25, 2003, 4:41:02 PM, you wrote: RW> How Do, All? RW> Here's one unsuccessful attempt to add each $val during a for: RW> <? RW> for ($z = 0;$z < count($val);$z++) RW> RW> { RW> $v = $val + $val; RW> } RW> RW> print 'Total?: '.$v.'<br>'; ?>> RW> Conversely I need to isolate certain elements oof a foreach: RW> <? RW> foreach($var as $v){ RW> if ($v != 'argument') RW> { RW> //add all the $v's for a total RW> } ?>> RW> Thank you! RW> -- I assume $val is an array of numbers?: <? $v = 0; for ($z = 0;$z < count($val);$z++){ $v += $val[$z]; } print 'Total?: '.$v.'<br>'; ?> Conversely I need to isolate certain elements oof a foreach: $total = 0; <? foreach($var as $v){ if ($v != 'argument') { $total += $v; } } ?> -- regards, Tom

« previous php.general (#140923) next »