I'm trying to understand something about arrays in for each loops that might be obvious to many.
When i loop through my multi-dimensional array, i attempt to find sub arrays with no third element. If they have no third element, i want to add a third element to that sub array with a specific value.
$testArray = array (
array("Green", "Yellow", "Blue"),
array("Brown", "Silver"),
array("Orange", "Pink", "Black"),
);
When i use the foreach loop:
foreach ( $testArray as $key => $array ) {
if (count($array) == '2') {
$array[] = "None";
};
}
No errors are thrown but nothing happens. When i use the for each loop:
foreach ( $testArray as $key => $array ) {
if (count($array) == '2') {
$testArray[$key][] = "None";
};
}
It works as expected.
Sorry for the long preamble, my questions is:
Why aren't these two foreach loops doing the same thing? Thanks!