0

I have been working on trying to retrieve specific results from a PHP for loop without success.

My objective is to run a loop on an array and to pull the specific value each time if it exists within the array. I am able to run the first loop and retrieve a value, however, when I run the second for function it retrieves the exact same number. I haven't been able to figure out why. E.g. The first loop returns the value of 6, but so does the second loop. I am trying to retrieve 6 if it exists from the first loop and 7 if it exists in the second loop. 7 is located in the array, so it should return 6 in the first for statement and 7 on the second.

Here is what I have done so far:

for ($i = 0; $i < count($final_data); $i++) {
    $check_sector = $final_data[$i]['sector'];
    $check_image = $final_data[$i]['image'];
      if($final_data[$i]['sector'] == 6){
        echo "<div id='6' class='w3-button w3-ripple grid-item-sector' onclick='getSector(this.id)'>"
        . "<div id='ss6' style='position: absolute; width: 100px; height: 100px; background-image: url(" . $check_image . ");'></div>"
                ."<div id='s6' class='overlay' ></div></div>";
        break;
      }else{
        echo "<div id='6' class='w3-button w3-ripple grid-item-sector' onclick='getSector(this.id)'>"
        ."<div id='s6' class='overlay' ></div></div>";
        break;
      }
  }

  for ($i = 0; $i < count($final_data); $i++) {
    $check_sector = $final_data[$i]['sector'];
    $check_image = $final_data[$i]['image'];
      if($final_data[$i]['sector'] == 7){
        echo "<div id='7' class='w3-button w3-ripple grid-item-sector' onclick='getSector(this.id)'>"
        . "<div id='ss7' style='position: absolute; width: 100px; height: 100px; background-image: url(" . $check_image . ");'></div>"
                ."<div id='s7' class='overlay' ></div></div>";
        break;
      }else{
        echo "<div id='7' class='w3-button w3-ripple grid-item-sector' onclick='getSector(this.id)'>"
        ."<div id='s7' class='overlay' ></div></div>";
          break;
      }
  }

4
  • hmmm it looks correct. I would try debugging it by doing something like var_dump($final_data); right before it starts echoing to see what it's doing under the hood.. Also how are you concluding it's doing 6 both times? Are you looking at the ID of the echoed div? Commented May 21, 2020 at 3:29
  • I have been using print_r to see what the value is when it runs to figure out what the result is. Here are the var_dump results for $final_data when I put the var_dump before the echo: array(2) { [0]=> array(2) { ["sector"]=> int(6) ["image"]=> string(12) "img/iron.png" } [1]=> array(2) { ["sector"]=> int(7) ["image"]=> string(12) "img/iron.png" } Commented May 21, 2020 at 3:49
  • And how are you determining it’s doing 6 both times? I noticed onclick is passing in “this.id” what is that?? Commented May 21, 2020 at 3:57
  • The first function catches the 6 and echos the first statement, the second function moves to the else statement which I believe means that it didn't catch the 7. Commented May 21, 2020 at 4:03

1 Answer 1

1

When you get to the second foreach loop, it’s hitting the first element in the array first.

So it’s saying is 6==7? No, so it goes to the else echo statement.

Then you have a break so it gives up and never checks the second element in the array which is the 7.

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

2 Comments

Essentially break means break out of this loop and don’t check anymore of the elements
If instead you meant continue on to the next iteration of this loop, you would want to replace “break;” with “continue;”

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.