我将两个数组合并在一起,它们都包含一个字符串(url)和int(score)。 以下是 Outome 的示例。每当字符串重复时,我都需要删除该字符串及其相应的 int。例如,在第 4 行 (www.thebeatles.com/- 30) 应删除。第 5 行和第 6 行也应删除,因为它们已经以不同的分数出现。
http://www.thebeatles.com/ - 55
http://en.wikipedia.org/wiki/The_Beatles - 49
http://www.beatlesstory.com/ - 45
http://www.thebeatles.com/ - 30
http://en.wikipedia.org/wiki/The_Beatles - 28
http://www.beatlesstory.com/ - 26
http://www.beatlesagain.com/ - 24
http://www.thebeatlesrockband.com/ - 23
http://www.last.fm/music/The+Beatles - 22
http://itunes.apple.com/us/artist/the-beatles/id136975 - 20
http://www.youtube.com/watch?v=U6tV11acSRk - 18
http://blekko.com/ws/http://www.thebeatles.com/+/seo - 17
http://www.adriandenning.co.uk/beatles.html - 16
http://www.npr.org/artists/15229570/the-beatles - 15
http://mp3.com/artist/The%2BBeatles - 14
http://www.beatles.com/ - 13
http://www.youtube.com/watch?v=TU7JjJJZi1Q - 12
http://www.guardian.co.uk/music/thebeatles - 11
http://www.cirquedusoleil.com/en/shows/love/default.aspx - 9
http://www.recordingthebeatles.com/ - 7
http://www.beatlesbible.com/ - 5
我是PHP的新手,我让array_unique()工作的最大努力失败了。真的很感谢一些帮助的人!
这是一个合并两个数组并丢弃任何重复项的函数,希望对您有所帮助:
function merge_links($arr_l, $arr_r) {
$new_links = array();
$links = array_merge($arr_l, $arr_r); //the big list with every links
foreach($links as $link) {
$found = false; //did we found a duplicate?
//check if we already have it
foreach($new_links as $new_link) {
if($new_link['url'] == $link['url']) {
//duplicate
$found = true;
break;
}
}
//not found, so insert it
if(!$found) {
$new_links[] = $link;
}
}
return $new_links;
}
$arr1[0]['url'] = 'http://test.nl';
$arr1[0]['score'] = 30;
$arr1[1]['url'] = 'http://www.google.nl';
$arr1[1]['score'] = 30;
$arr2[0]['url'] = 'http://www.tres.nl';
$arr2[0]['score'] = 30;
$arr2[1]['url'] = 'http://test.nl';
$arr2[1]['score'] = 30;
print_r(merge_links($arr1, $arr2));
您可以将链接作为包含链接和分数的数组的键。与键相对应,将始终有一个值。但是在最后一个中添加的那个将出现在您的最终数组中。
好吧,即使在技术上,这些字符串也不是唯一的。 即它们完全不同。
- http://www.thebeatles.com/- 55
- http://www.thebeatles.com/- 30
因此,array_unique() 不会为您提供所需的输出。解决此问题的一种方法是定义一个单独的数组并分别存储 URI 和数字。一个可管理的形式是这样的。
array(
array("http://www.thebeatles.com", 55),
array("http://www.thebeatles.com", 30)
);