从两个多维数组中删除重复项

时间:2015-11-15 14:45:55

标签: php arrays sorting multidimensional-array unique

我遇到了一个问题,试图从两个两维多维数组中获取一个唯一的数组,但未能这样做。

我尝试过搜索解决方案,但我能做到的只是一个数组。

这是两个阵列。

Array
(
    [1] => Array
        (
            [title] => Strongest Links - Directory list
            [promotion] => At Strongest Links you can find good linking partners, track directory submission and manage your online promotion campaign. Try it for free.
            [domain] => http://www.strongestlinks.com
        )
)
Array
(
    [0] => Array
        (
            [title] => Strongest Links - Directory list
            [kbsize] => 88820
            [url] => http://www.strongestlinks.com/directories.php
            [meta_description] => At Strongest Links you can find good linking partners, track directory submission and manage your online promotion campaign. Try it for free.
            [kbsize_t] => 88.82kb
        )

    [3] => Array
        (
            [title] => 
Strongest Links - Directory list
            [kbsize] => 20303
            [url] => http://www.strongestlinks.com/directories/369
            [meta_description] => At Strongest Links you can find good linking partners, track directory submission and manage your online promotion campaign. Try it for free.
            [kbsize_t] => 20.303kb
        )

    [4] => Array
        (
            [title] => Strongest Links - Directory list
            [kbsize] => 20366
            [url] => http://www.strongestlinks.com/directories/317
            [meta_description] => At Strongest Links you can find good linking partners, track directory submission and manage your online promotion campaign. Try it for free.
            [kbsize_t] => 20.366kb
        )

    [5] => Array
        (
            [title] => SmartLinks.org - News, Reference, Facts - QuickLinks
            [kbsize] => 95526
            [url] => http://www.smartlinks.org
            [meta_description] => SmartLinks.org - QuickLinks/Favorites - News, Reference, Facts and Topics organized by Categories.
            [kbsize_t] => 95.526kb
        )

)

我正在尝试创建一个函数,将数组1中的url字段与数组2中的域字段进行比较,并返回数组1减去也在数组2中找到的元素。

如果它可以将数组1中的字段url与数组2中的域字段进行比较,那么更好的功能也是如此,并返回两个字段匹配的数量。

1 个答案:

答案 0 :(得分:3)

你想这样做:

  1. 制作一个查找表,您可以快速检查是否应该排除某个域而无需搜索整个数组
  2. 创建一个带有网址并返回其域名的函数
  3. 创建一个新数组,插入存在于array2中的项目,只要这些域不在您排除域的查找表中。
  4. 以下是代码:

    // the array of domains that we want to exclude
    // you can still have your other properties too, I just
    // excluded them to make the example cleaner :)
    $array1 = [
        [ 'domain' => 'http://www.strongestlinks.com' ]  
    ];
    
    // the array of urls
    $array2 = [
        [ 'url' => 'http://www.strongestlinks.com/directories.php' ],
        [ 'url' => 'http://www.strongestlinks.com/directories/369' ],
        [ 'url' => 'http://www.smartlinks.org' ] 
    ];
    
    // build a lookup table of domains that we want to subtract from array2
    $blacklisted_domains = [];
    foreach($array1 as $v) {
      $blacklisted_domains[$v['domain']] = true;
    }
    
    // small function to take a url and return its domain
    function get_domain($url) {
      $parts = explode('/', $url);
      return $parts[0] . '//' . $parts[2];
    }
    
    // array3 will contain array2 - array1
    $array3 = [];
    
    // for each url in array2, find its domain and insert the
    // url into array3 if the domain isnt in blacklisted_domains
    foreach($array2 as $v) {
      $domain = get_domain($v['url']);
      if(!isset($blacklisted_domains[$domain])) {
        $array3[] = $v;
      }
    }