The following is the best way to iterate through and modify array in PHP5.*+
(source: http://zend-php.appspot.com/questions_list -- question #3)
$array = array("one" => 1, "two" => 2, "three" => 3, "four" => 4, "five" => 5);
foreach($array as $key => &$val) {
$val += 1;
}
What is it about this method of iterating and modifying that makes it better than others?
Can someone please explain how come this actually works?
This iterates just fine, why include as $key => &$val
if we are not using the keys?, also why do we need to include the &
in &$val
:
foreach($array as $val){
echo $val;
}
I appreciate the explanation, Thanks in advance!