THE QUESTION
Why is the line with "l++;"
impacting the loop counts of the foreach loop? That line is merely there as a counter but somehow it is impacting the end result.
Can someone please help explain what is going on here?
THE CODE
I have the following multidimensional array:
$team_array = array(
array(68, 6, 0, 10, "1.000"),
array(72, 6, 0, 10, "1.000"),
array(65, 6, 0, 8, "1.000"),
array(62, 6, 0, 4, "1.000"),
array(54, 3, 3, 3, ".500"),
array(55, 3, 3, -5, ".500"),
array(59, 0, 6, -16, ".000")
);
I am using the following code to loop over the arrays.
$foo = "1.000";
$k = 0;
$l = 0;
$m = 0;
$n = 0;
foreach($team_array as $index => $inner_arr)
{
foreach($inner_arr as $value)
//echo $value.", ";
//echo "<br/>";
$l++;
{
$m++;
echo "index - $index<br>";
echo "perc = ".$team_array[$index][4]."<br>";
if ($team_array[$index][4] == $foo)
{
$tie_tm[] = $team_array[$index][0];
$n++;
}
}
$k++;
}
echo "k - $k<br>";
echo "l - $l<br>";
echo "m - $m<br>";
echo "n - $n<br>";
echo "<pre>";
print_r($tie_tm);
I have left the debugging bits in as they show the strange (to me) behavior. Specifically, if I comment out the line "l++;"
I get LOTS more loops than I would expect. Running this code without the "l++;"
line give me this:
k - 7
l - 0
m - 35
n - 20
Array
(
[0] => 68
[1] => 68
[2] => 68
[3] => 68
[4] => 68
[5] => 72
[6] => 72
[7] => 72
[8] => 72
[9] => 72
[10] => 65
[11] => 65
[12] => 65
[13] => 65
[14] => 65
[15] => 62
[16] => 62
[17] => 62
[18] => 62
[19] => 62
)
With it left in, I get:
k - 7
l - 35
m - 7
n - 4
Array
(
[0] => 68
[1] => 72
[2] => 65
[3] => 62
)
This is the result I want and expected.
In the end, what I need is exactly what I am getting in the final result with 4 elements in the array. I would like to remove the debugging bits but need to understand how I might need to alter this code to keep my end result.
Thanks in advance!