0

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!

1
  • 1
    foreach($inner_arr as $value) $l++; { ... } this is almost certainly a typo. You probably meant foreach($inner_arr as $value) { $l++; ... } Commented Apr 27, 2014 at 16:06

4 Answers 4

1

Reframed your code a bit. Try -

$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")
      );

$result = array();
for($i=0; $i<4; $i++)
{
    $result[] = $team_array[$i][0];
}

print_r($result);


OUTPUT

Array
(
   [0] => 68
   [1] => 72
   [2] => 65
   [3] => 62
)


DEMO

http://3v4l.org/RJu69#v430

Sign up to request clarification or add additional context in comments.

2 Comments

You don't explain the problem with the existing code, which is "the question" and probably, in this instance, more useful to the OP.
Yes I know. Still thought that too much of OP's code is not useful, since it can be done in one simple for loop
1

Remove the code between your foreach header and the opening curly brace.

What you've inadvertently done is create a foreach loop that actually works like this

foreach($inner_arr as $value) 
    $l++;

When what you want is a foreach loop that looks like this

foreach($inner arr as $value) {
    // all your looping code should go in between the curly braces 
    // and the curly braces need to immediately follow the foreach header (with nothing in between)
}

Comments

1

You have put l++ between your second foreach and its corresponding {}. So in stead of foreaching your code between the {}, you are only foreaching the l++. Try placing your l++ before the second foreach or inside the corresponding {}.

Comments

0

Thanks for the quick feedback. I really appreciate it!!!

The answers helped me realize that the inner foreach loop was really doing nothing for me. I am only interested in looking in a single designated dimension of the multidimensional array and was getting my desired result without the use of the inner foreach loop.

I modified the code as follows:

$foo = "1.000";
$m = 0;
$n = 0;
foreach($team_array as $index => $inner_arr)
{
    $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++;
     }   
}

echo "m - $m<br>";
echo "n - $n<br>";


echo "<pre>";
print_r($tie_tm);

Again... thanks for all the feedback. This community is incredible!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.