There is nothing wrong with your foreach. There is something wrong with your understanding of how PHP parses input-attributes (_POST, _GET).
<input type="text" name="foobar" value="one">
<input type="text" name="foobar" value="two">
<input type="text" name="foobar" value="three">
translates to the application/x-www-form-urlencoded representation foobar=one&foobar=two&foobar=three.
PHP parses this string into a map (associative array). It does this somewhat like the following code:
<?php
$_GET = array();
$string = 'foobar=one&foobar=two&foobar=three';
$parts = explode('&', $string);
foreach ($parts as $part) {
$p = explode('=', $part);
$_GET[urldecode($p[0])] = urldecode($p[1]);
}
So basically it is assigning $_GET['foobar'] three times, leaving $_GET['foobar'] === 'three'.
Translated, this is what is happening here:
$_GET['foobar'] = 'one';
$_GET['foobar'] = 'two';
$_GET['foobar'] = 'three';
At this point I'd like to note that other languages (Ruby, Java, …) deal with this quite differently. Ruby for example recognizes the repeating key and builds something similar to $_GET['foobar'] = array('one', 'two', 'three').
There is a simple "trick" to tell PHP that the repeating value should be parsed into an array:
<input type="text" name="foobar[]" value="one">
<input type="text" name="foobar[]" value="two">
<input type="text" name="foobar[]" value="three">
will lead to $_GET['foobar'] = array('one', 'two', 'three');
Translated, this is what is happening here:
$_GET['foobar'][] = 'one';
$_GET['foobar'][] = 'two';
$_GET['foobar'][] = 'three';
(Note: $array[] = 'value' is the same as array_push($array, 'value'))
So whenever you're dealing with repeating key names (or <select multiple>) you want to add [] to the name, so PHP builds an array from it.
You may also want to know that you can actually specify the array-keys:
<input type="text" name="foobar[hello][world]" value="one">
will lead to $_GET['foobar']['hello']['world'] == 'one'.
print_r($_POST);