2015-11-15 103 views
0

删除duplicats我跑进试图从二两个多维数组获得一系列独特的问题,没能如愿。从两个多维数组

我试过在寻找解决方案,但我所能的事情是只有一个阵列。

这里有两个阵列。

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场域从阵列2比较URL字段的功能,并返回数组1负的元素,其也在阵中发现2

更妙函数也可以是,如果它可以比较数组1中的字段url和数组2中的域字段,并返回两个字段匹配的数量。

+0

什么是比较域? –

+1

将“pastie”的内容粘贴到您的问题中。点击[编辑]。 – hjpotter92

+0

已编辑。对不起,通过这将是很大的,如果我加入了阵列 – Convertor

回答

3

你想这样做:

  1. 使查找表,您可以快速地检查域应该无需搜索整个数组
  2. 做一个函数,一个URL和被排除在返回其领域
  3. 作出新的数组,插入存在于数组2,只要这些领域不是排除域的查找表项目。

下面的代码:

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

完美,完全就是我一直在寻找。非常感谢你。 – Convertor