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

Remove duplicats from two multidimensional array

我 运行 遇到一个问题,试图从两个二维多维数组中获取唯一数组,但失败了。

我已经尝试搜索解决方案,但我所能做的只是关于一个数组。

这是两个数组。

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 中的域字段,并且 returns 数组 1 减去数组 2 中也存在的元素。

如果它可以将数组 1 中的字段 url 与数组 2 中的域字段进行比较,并且将 return 与两个字段的匹配程度进行比较,则功能会更好。

您想这样做:

  1. 进行查找 table,您可以在其中快速检查是否应排除某个域,而无需搜索整个数组
  2. 创建一个接受 url 和 returns 域的函数
  3. 创建一个新数组,插入 array2 中存在的项目,只要这些域不在您查找 table 排除域中即可。

代码如下:

// 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;
  }
}