Well, the code you have looks totally reasonable to me.
foreach() is a looping construct that iterates across each item in the array and sets variables containing the index and value for each iteration.
For example, if you had an array:
$cities = array("Mumbai", "Kolkatta");
foreach($cities as $city)
{
echo "You'd work in $city.\n";
}
would print "You'd work in Mumbai.\n" in the first iteration and "You'd work in Kolkatta.\n" on the second.
http://www.php.net/foreach
As such, your code looks fine -- $_POST['pref_cities'] is an array, just like $cities is in my little example above.
The only thing I can think of is that you're getting this error before the user submits the form. If $_POST doesn't exist, then you'll get an "undefined index" warning telling you that the $_POST array doesn't contain an index named "pref_cities".
The other thing I should talk about is what the empty brackets ("[]") mean. PHP uses this notation to append items to the end of an array. Empty brackets will create a new numeric index where the index is equal to 1 + <the greatest existing numerical index>.
In your HTML form, you have an input who's name is "pref_cities[]". If you look at the HTTP POST header information, you'll see that each city in a multiple selection is set as a distinct variable. That gets interpreted by PHP as individual assignments. The PHP version of this would be:
$pref_cities[] = <city 1>;
$pref_cities[] = <city 2>;
...
$pref_cities[] = <city n>;
So you see, this continually appends each city to the end of the $pref_cities array.
That's important to know -- the name of the array variable is "pref_cities", _NOT_ "pref_cities[]".
Hope this helps clear things up!
Take care,
Nik
http://www.bigaction.org/