0

我有一个包含各种站点链接的多维数组,这是输出:

Array
(
    [0] => Array
        (
            [0] => http://www.msn.com/etc
            [1] => http://www.yahoo.com/etc
            [2] => http://www.google.com
        )

    [1] => Array
        (
            [0] => http://www.abc.com/etc
            [1] => http://www.hotmail.com/etc
            [2] => http://www.hotmail.com/page/2
        )

    [2] => Array
        (
            [0] => http://www.live.com/etc
            [1] => http://www.google.com/etc
            [2] => http://www.stock.com
        )

)

我想匹配多个 URL,这里是我的示例代码:

$sites = array("msn.com","hotmail.com","live.com");
$links = array(
    array("http://www.msn.com/1","http://www.yahoo.com/etc","http://www.google.com"),
    array("http://www.msn.com/2","http://www.hotmail.com/","http://www.hotmail.com/page/2"),
    array("http://www.live.com/etc","http://www.google.com/etc","http://www.stock.com")
);

我需要 $sites 中的任何站点,首先它会从 $links 数组中找到 msn.com 站点,因此如果它在第一个数组($links[0])中找到 msn.com,它将不会在其他 $links 中搜索 msn.com数组但继续搜索其他(hotmail.com 和 live.com),如果它在一个数组中找到同一主机的 2 个链接,它将加入它们,这意味着如果它在一个数组元素中找到主机,它将不会搜索该主机在 $links 数组的其他元素中,因此上面的最终输出将是:

Array
(
    [msn] => Array
        (
            [0] => http://www.msn.com/1
        )

    [hotmail] => Array
        (
            [0] => http://www.hotmail.com/
            [1] => http://www.hotmail.com/page/2
        )

    [live] => Array
        (
            [0] => http://www.live.com/etc
        )

)

我不确定如何执行此任务,我将不胜感激任何输入。谢谢

4

2 回答 2

3

最好我给你的是伪代码。

for each of the links arrays
    for each of the non eliminated sites
        find all the matching entries in this link array, for this site
        if theres at least one match
            eliminate this site from the list
            store the matches into a results array indexed by sitename, 
            --for example $results[$sitename] = $matchesArray

玩得开心

于 2012-05-13T15:48:25.793 回答
-1

这是完整的代码:

$final = array();
foreach($sites as $site)
{
    $found = false;
    foreach($links as $link_batch)
    {   
        foreach($link_batch as $link)
        {
            if(strpos($link, $site))
            {
                $found = true;
                $final[$site][] = $link;
            }
        }
        if($found)
        {
            break;
        }
    }
}
print_r($final);
于 2012-05-13T15:59:05.267 回答