View Full Version : for each(list($var, $val) = each($HTTP_POST_VARS))
Phantom
04-29-2003, 09:58 PM
I've seen this used a lot, and the best explanation that I got (probably a good explanation, just not in terms I understand) was "it splits an array into variables"...what does this do? :-(
it loops through an array ("collection") and lets you run some code for each element. Say your user posts a form and you want to print (or validate, or include them in an sql statement or ...) all names of the formfields and there values, well then this function will start with the first formfiel, run the print/validate/... code and then move on to the next, till all elements are processed.
Here's some more info + lots of exaples.
http://www.php.net/manual/en/control-structures.foreach.php
firepages
04-30-2003, 02:38 PM
you appear to have a mixture of 2 types of array iteration there ?
<?
while(list($key,$var)=each($array)){
echo "$key , $var<br>";
}
?>
or perhaps simpler
<?
foreach($array as $key->$var){
echo $key.' '.$var.'<br />';
}
?>
each of the above loop though $array which may look like
$array['key']='var';
$array['key2']='var2';
//etc//
or
$array[0]='var';//here '0' is the 'key'
the 'list' ... 'each' looks confusing but was useful before foreach() was available (and still is !), check out list() and each() in the manual for more info.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.