duanhuilao0787 2016-01-16 12:17 采纳率: 100%
浏览 32
已采纳

(针,干草堆)与2阵列,困惑

Have been trying to figure out this simple problem for 3 days and don't understand why the function removes only some of the values, but leaves others in place.

This is function checks a list of bad domains against a list of good domains, if it finds a bad domain, it removes it from the good domains list.

Here's my code:

// check each bad domain, against each array in good 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 some reason it 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 it removes all http://twitter.com/?lang=tic-toc, but leaves all http://twitter.com/?lang=KA-BOOM..

Why does it do that? I tried play with array_values, but it still doesn't work.

Sorry for the silly array values, just wanted it to stand out so it's more clear. Appreciate your help.

  • 写回答

2条回答 默认 最新

  • duanping3587 2016-01-16 12:42
    关注

    Use simple foreach loops, because that way you don't have to use array_values() function. So your remove_excluded_domains() funciton should be like this:

    function remove_excluded_domains($good_domains, $bad_domains) {
        foreach($bad_domains as $bad_domain){
            foreach($good_domains as $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 you want the array to be indexed numerically then use array_values() function on the returned array, like this:

    $spider_array = array_values(remove_excluded_domains($good_domains, $bad_domains));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错