Page 1 of 1
foreach help
Posted: Sun Mar 07, 2004 4:52 pm
by Steveo31
Could someone give me some info on the foreach function? I looked it up in php.net, but I don't know just how it goes through.
Code: Select all
$arr = array("one", "two", "three");
//set up array
reset ($arr);
//start at the beginning of the array
foreach ($arr as $value) {
echo "Value: $value<br />\n";
}
/*
--Output--
Value: one
Value: two
Value: three
*/
?>
PHP.net:
foreach (array_expression as $key => $value) statement
And the => assigns the value, right?
-S
Posted: Sun Mar 07, 2004 5:05 pm
by markl999
If you have :
$array = array(
'foo' => 'bar',
'foo2' => 'bar2'
);
then
foreach($array as $key=>$value){
echo $key.' '.$value.'<br />';
}
will show
foo bar
foo2 bar2
ie foo and foo2 are the keys, bar and bar2 are the values. In a simple array like $array = array('one', 'two', 'three') then the key is the element index (0, 1 and 2) and the values are 'one', 'two' and 'three'.
Posted: Sun Mar 07, 2004 5:43 pm
by Steveo31
Ahh okay. So the $array part in the foreach function is the "object" to look in, the key and values is the stuff in the array (in this example) and the echo part is just displaying it. Got it. I guess the "as" part is throwing me off.
I don't know where it is on my comp, or whatnot, but there was an example that used this foreach, but had $_POST in the first spot for the foreach, something like:
Code: Select all
foreach($_POST as $key=>$value){
//
}
But I can't remember. What do you think about this in terms of useage?
Posted: Sun Mar 07, 2004 5:46 pm
by markl999
Yeah, looks fine. Just be aware that when using foreach() on $_POST like that you usually want to 'ignore' the submit button (and possible other keys) and concentrate on the actual form fields/values.
You can 'ignore' the submit button by doing something like,
if($key != 'submit'){ .. process the other keys/values here .. } inside the foreach bit.
Posted: Sun Mar 07, 2004 5:51 pm
by Steveo31
Yeah, looks fine. Just be aware that when using foreach() on $_POST like that you usually want to 'ignore' the submit button (and possible other keys) and concentrate on the actual form fields/values.
Could you elaborate a bit on this please?
Thanks for your help thus far. Appreciate it.

Posted: Sun Mar 07, 2004 5:57 pm
by markl999
Well, lets say you want to 'do something' with the form values that were submitted, you usually don't want to do anything with the submit button (which will appear in the $_POST array). So if for example you were writing some test code that displayed all the form names and values posted then you might do something like :
Code: Select all
<?php
//first check if the submit button was pressed
// *you'de normally use another element for this, but i'm checking the submit
// button for simplicities sake
if(!empty($_POST['submit'])){
foreach($_POST as $key=>$val){
if($key != 'submit'){ //ignore the submit button
echo $key.' '.$val.'<br />';
}
}
} else {
//this is were you might put the forms html
}
?>
This will ignore the submit button in the output as you usually don't care about processing it, you only care that it was pressed in the first place.
Posted: Sun Mar 07, 2004 8:44 pm
by Steveo31
Interesting....
Thanks for stickin with it
