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.
$linksstring, 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.