Jonas Tamoševičius
Jonas Tamoševičius

Reputation: 185

PHP remove array inside foreach loop

so I am trying to remove array in if statement inside foreach loop...

<?php
        foreach ($politics as $tag => $key):?>


        <?php    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
                    $ip = $_SERVER['HTTP_CLIENT_IP'];
                    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
                    } else {
                        $ip = $_SERVER['REMOTE_ADDR'];
                      }
                      $vienodas = "SELECT * FROM pol WHERE (politikas = '$Veryga' AND ip = '$ip')";
                      $vienodasres = mysqli_query($conn, $vienodas);
                     if ( mysqli_num_rows($vienodasres) > 0){
                         //IT DOESN'T WORK 
                         unset($politics[$tag]);
                         //IT DOESN'T WORK 
                     }
                      ?>
 <div class="mySlides">
<?php $Veryga= $key['vardas'];
 $Veryga2 = str_replace(' ', '', $Veryga);
 ?>
  <div <?php echo 'id="'.$Veryga2.'"'; ?> class="politikai">

<..Not important code..>



  </div>
  <?php
  endforeach; ?>

It doesn't want to work, I was trying to do it, the if statement just checks if there are lines which is equal to user IP and the slide which they are

Upvotes: 0

Views: 124

Answers (2)

Dominik
Dominik

Reputation: 111

you're not accessing that $politics array-element later, so it is unset, but the control flow continues inside the element. if you'd like to prevent the output use a continue; statement at that point.

Upvotes: 0

Andrew Schultz
Andrew Schultz

Reputation: 4243

You have the value and key elements around the wrong way it should be defined like this.

foreach ($politics as $key => $tag):
    unset($politics[$key]);

Upvotes: 1

Related Questions