0

Array_insersect 只有在数组内部字符串完全匹配时才会显示结果。但我需要获得类似的记录而不是确切的记录。

$array1 = array("http://example.com/abc","http://google.com/xyz","http://yahoo.com/abc", "http://stackoverflow.com/mnr");
$array2 = array("http://example.com/xyz","http://w3schools.com/xyz","http://stackoverflow.com/abc");
$similarRecords = array_intersect($array1, $array2);

它会给一个空的。

我在找这样的

$similarRecords = array("example.com");

我怎么能得到这个。请建议我

4

7 回答 7

1

没有找到“相似”记录的内置方法(主要是因为“相似”有很多定义!)。但是,您可以手动执行此操作,方法是遍历其中一个数组,并查找另一个数组中是否有类似的元素。

function keepSimilars (array $first, array $second) {
  $result = array();
  foreach ($first as $elem) {
    $base = getBase($elem);
    $found = false;
    foreach ($second as $elem2)
      if ($base = getBase($elem2)) {
        $found = true;
        break;
      }
    if ($found) $result[] = $base;
  }
  return $result;
}

当然,其中getBase()将是一个函数(您必须定义),它为您提供字符串的基本部分,您想要匹配的部分(getBase('http://example.com/abc')应该给您example.com)。您也可以使用回调。

于 2013-03-01T10:00:28.523 回答
1

你在找这个吗?

$array1 = array("http://example.com/abc", "http://google.com/xyz", "http://yahoo.com/abc", "http://stackoverflow.com/mnr");
$array2 = array("http://example.com/xyz", "http://w3schools.com/xyz", "http://stackoverflow.com/abc");

$newArray1 = array();
foreach ($array1 as $array1data) {
  $parse1 = parse_url($array1data);
  $newArray1[] = $parse1['host'];
}

$newArray2 = array();
foreach ($array2 as $array2data) {
  $parse2 = parse_url($array2data);
  $newArray2[] = $parse2['host'];
}

$similarRecords = array_intersect($newArray1, $newArray2);

print_r($similarRecords);

输出

Array ( [0] => example.com [3] => stackoverflow.com ) 
于 2013-03-01T10:03:17.593 回答
1

您需要进行一些字符串处理,只从每个字符串中提取域名并进行比较。

这有效:

$array1 = array("http://example.com/abc","http://google.com/xyz","http://yahoo.com/abc", "http://stackoverflow.com/mnr");
$array2 = array("http://example.com/xyz","http://w3schools.com/xyz","http://stackoverflow.com/abc");

$similarRecords = array_intersect(array_map("parseURLs", $array1), array_map("parseURLs", $array2));
print_r($similarRecords);

function parseURLs($strURL) {
  $parts = parse_url($strURL);
  return $parts['host'];
}

输出是:

Array ( [0] => example.com [3] => stackoverflow.com ) 
于 2013-03-01T10:03:26.230 回答
1

试试这个:

function domain($n){
    $url = parse_url($n);
    return $url['host'];
}

$array1 = array("http://example.com/abc","http://google.com/xyz","http://yahoo.com/abc", "http://stackoverflow.com/mnr");
$array2 = array("http://example.com/xyz","http://w3schools.com/xyz","http://stackoverflow.com/abc");

$similarRecords = array_intersect(array_map("domain", $array1), array_map("domain", $array2));

echo "<pre>";
print_r($similarRecords);

输出 :

Array
(
    [0] => example.com
    [3] => stackoverflow.com
)
于 2013-03-01T10:05:52.907 回答
1

您可以使用similar_text 进行比较。唯一的问题是可比性因素似乎是任意的。

<?php
$array1 = array("http://example.com/abc","http://google.com/xyz","http://yahoo.com/abc", "http://stackoverflow.com/mnr");
$array2 = array("http://example.com/xyz","http://w3schools.com/xyz","http://stackoverflow.com/abc");
print_r($array1);
print_r($array2);
echo "\n";

foreach ($array1 as $slice)
{
        foreach ($array2 as $slice2)
        {
                echo 'Comparing '.$slice.' to '.$slice2.' gives comparability of '.similar_text($slice,$slice2)."\n";
                if (similar_text($slice,$slice2) > 18)
                {
                        echo $slice.' ____is similar to___ '.$slice2."\n";
                }
        }
}
?>

输出:

Comparing http://example.com/abc to http://example.com/xyz gives comparability of 19
http://example.com/abc ____is similar to___ http://example.com/xyz
Comparing http://example.com/abc to http://w3schools.com/xyz gives comparability of 13
Comparing http://example.com/abc to http://stackoverflow.com/abc gives comparability of 17
Comparing http://google.com/xyz to http://example.com/xyz gives comparability of 17
Comparing http://google.com/xyz to http://w3schools.com/xyz gives comparability of 18
Comparing http://google.com/xyz to http://stackoverflow.com/abc gives comparability of 14
Comparing http://yahoo.com/abc to http://example.com/xyz gives comparability of 13
Comparing http://yahoo.com/abc to http://w3schools.com/xyz gives comparability of 15
Comparing http://yahoo.com/abc to http://stackoverflow.com/abc gives comparability of 18
Comparing http://stackoverflow.com/mnr to http://example.com/xyz gives comparability of 14
Comparing http://stackoverflow.com/mnr to http://w3schools.com/xyz gives comparability of 16
Comparing http://stackoverflow.com/mnr to http://stackoverflow.com/abc gives comparability of 25
http://stackoverflow.com/mnr ____is similar to___ http://stackoverflow.com/abc

因此,您可以将匹配项放在一个数组中并在之后输出。

于 2013-03-01T10:13:31.330 回答
0

您可能会使用 array_search() 或 array_uintersect() 循环遍历一个数组并在另一个数组上进行松散搜索,或者使用回调 strpos 的 uintersect

于 2013-03-01T10:00:00.570 回答
0

多亏了这一切,我终于做到了。

<?php
$array1 = array("http://example.com/abc","http://google.com/xyz","http://yahoo.com/abc", "http://stackoverflow.com/mnr");
$array2 = array("http://example.com/xyz","http://w3schools.com/xyz","http://stackoverflow.com/abc");
echo "<pre>";
print_r($array1);
echo "<br/>";
print_r($array2);
echo "<br/>";
$similarRecords = similarRecords($array1, $array2);
print_r($similarRecords);
echo "</pre>";
function getDomain($strURL)
{
    $url = parse_url($strURL);
    return $url['host'];
}

function similarRecords($array1, $array2)
{
    $similarRecords = array();
    foreach ($array1 as $slice)
    {
        foreach ($array2 as $slice2)
        {
            if (getDomain($slice) == getDomain($slice2))
            {
                $similarRecords[] = $slice; 
                $similarRecords[] = $slice2; 
            }
        }
    }

    return $similarRecords;
}
?>
于 2013-03-04T08:13:15.247 回答