0

i try to foreach on array that comes from explode()

i have this:

     $links = " http://hi.co.il<br>
        ht tp://hi.co.il<br>
   http://hi.co.il<br>
   http://hi.co.il<br>
   http://hi.co.il<br>
   http://mega.co.il<br>
   http://hi.co.il";

   $links = explode('<br>',trim($links));
   //$links = preg_replace('/\s+/', '', $links);
   $currenturl = "http://mega.co.il";

   //if(($key = array_search($currenturl, $links)) !== false) {
  //  unset($links[$key]);
//}

    foreach($links as $key => $value):

     if($value == $currenturl):
         unset($links[$key]);
     endif;

    endforeach;

   print_r($links);

$links printed before foreach:

Array ( [0] => http://hi.co.il [1] => ht tp://hi.co.il [2] => http://hi.co.il [3] => http://hi.co.il [4] => http://hi.co.il [5] => http://mega.co.il [6] => http://hi.co.il )

$links printed after foreach:

http://hi.co.il ht tp://hi.co.il http://hi.co.il http://hi.co.il http://hi.co.il http://mega.co.il http://hi.co.il

why foreach take out from array back to string ? I want to check with foreach if its current url then unset from array, i am newbie and i can make it to work with array_search but i want to do it with foreach.

Thanks for answers and sorry for bad english.

2
  • I can't reproduce that issue. Is it possible for you to help reproduce it? Commented Oct 28, 2015 at 18:50
  • 1
    If you have the newlines in the original $links string, you would want to call $value = trim($value) on each iteration of the loop. Else, the newlines would be part of each string and not be an exact match. The initial trim will only remove whitespace from the entire string, not each value in the resulting array. Commented Oct 28, 2015 at 18:56

2 Answers 2

1

Try this:

$links = array_filter(
  $links,function($element) {
  return $element !== 'http://mega.co.il';
});

array_filter will remove any element where the return value of the function is false. For all elements, return value will be true, except the one that matches.

And miken32 is right about your : and endif stuff. Usually that is only used when your PHP code is sprinkled lightly in a mostly-HTML file. For pure PHP code, most people use the curly braces { and } along with indentation.

Considering JonathanKuhn's comment, you could also try this slight variation:

$links = array_filter(
  $links,function($element) {
  return strpos($element,'http://mega.co.il') !== true;
});

This will also give you the desired filtering effect, but it will not remove all newlines. To remove the newlines as well, you could do this:

array_walk($links,function($value,$key) {
  $value = trim($value);
});
$links = array_filter(
  $links,function($element) {
  return strpos($element,'http://mega.co.il') !== true;
});
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for alternate way but i prefer foreach as i need do more things on part of array keys.
@user2635001 You could also use array_walk for applying functions to each element of the array. I read somewhere that unsetting elements inside a foreach loop can cause unpredictable results, but I can't find the reference now. For that reason, I would often use a copy of the array in the foreach loop, then copy that array over the original array variable. Also, you should probably upvote miken's answer since you accepted it. ;)
0

This works just fine, nothing is turning the array into a string. You should be trimming your strings afterwards though, for your matching to work.

<?php

$links = "http://hi.co.il<br>
http://hi.co.il<br>
http://hi.co.il<br>
http://hi.co.il<br>
http://hi.co.il<br>
http://mega.co.il<br>
http://hi.co.il";

$links = explode('<br>',trim($links));
print_r($links);
$currenturl = "http://mega.co.il";

foreach($links as $key => $value) {
    if(trim($value) == $currenturl) {
        unset($links[$key]);
    }
}
print_r($links);
?>

I'd recommend not using alternate syntax for code blocks, these are generally meant for use in HTML templating and will probably get you into trouble.

2 Comments

I swear that didn't worked for me and then now i take your foreach and this working,thanks dude.
I would suggest using === instead of ==.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.