php - (Needle, Haystack) with 2 arrays, confused -
have been trying figure out simple problem 3 days , don't understand why function removes of values, leaves others in place.
this function checks list of bad domains against list of domains, if finds bad domain, removes domains list.
here's code:
// check each bad domain, against each array in array list $bad_domains = array('youtube.com', 'facebook.com', 'google.com', 'twitter'); $good_domains = array( 'http://www.wufoo.com/', 'https://plus.google.com/u/0/b/105790754629987588694', 'http://studioduplateau.com/ss=', 'http://twitter.com/?lang=tic-toc', 'http://twitter.com/?lang=ka-boom', 'http://twitter.com/?lang=tic-toc', 'http://twitter.com/?lang=ka-boom', 'http://twitter.com/?lang=tic-toc', 'http://twitter.com/?lang=ka-boom', 'http://twitter.com/?lang=tic-toc', 'http://twitter.com/?lang=ka-boom', 'http://twitter.com/?lang=ka-boom', 'lastofthemohicans.com' ); function remove_excluded_domains($good_domains, $bad_domains) { for($x=0; $x<count($bad_domains); $x++) { for($y=0; $y<count($good_domains); $y++) { if(strpos($good_domains[$y], $bad_domains[$x])) { unset($good_domains[$y]); $good_domains = array_values($good_domains); } } } return $good_domains; } $spider_array = remove_excluded_domains($good_domains, $bad_domains);
for reason returns:
[0] => http://www.wufoo.com/ [1] => http://studioduplateau.com/ss= [2] => http://twitter.com/?lang=ka-boom [3] => http://twitter.com/?lang=ka-boom [4] => http://twitter.com/?lang=ka-boom [5] => http://twitter.com/?lang=ka-boom [6] => lastofthemohicans.com
so removes http://twitter.com/?lang=tic-toc, leaves http://twitter.com/?lang=ka-boom..
why that? tried play array_values, still doesn't work.
sorry silly array values, wanted stand out it's more clear. appreciate help.
use simple foreach
loops, because way don't have use array_values()
function. remove_excluded_domains()
funciton should this:
function remove_excluded_domains($good_domains, $bad_domains) { foreach($bad_domains $bad_domain){ foreach($good_domains $key => $good_domain){ if(strpos($good_domain, $bad_domain) !== false){ unset($good_domains[$key]); } } } return $good_domains; } $spider_array = remove_excluded_domains($good_domains, $bad_domains);
note: if want array indexed numerically use array_values()
function on returned array, this:
$spider_array = array_values(remove_excluded_domains($good_domains, $bad_domains));
Comments
Post a Comment