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 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器