vvr02
vvr02

Reputation: 611

How to find similar records in two arrays PHP

Array_insersect will show result only when inside array the string is matched completely. But I need to get the similar records instead of exact records.

$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);

It will give an empty.

I looking for like this

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

How can i get this. Please suggest me

Upvotes: 0

Views: 177

Answers (7)

vvr02
vvr02

Reputation: 611

Thanks to all this is what I have done finally.

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

Upvotes: 0

uNople
uNople

Reputation: 554

You can use similar_text to compare. The only problem is the comparability factor seems arbitrary.

<?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";
                }
        }
}
?>

output:

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

so then you would put the matches in an array and output after.

Upvotes: 1

Prasanth Bendra
Prasanth Bendra

Reputation: 32740

Try this:

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);

Output :

Array
(
    [0] => example.com
    [3] => stackoverflow.com
)

Upvotes: 1

Husman
Husman

Reputation: 6909

You need to do some string processing and only pull out the domain name from each string and compare that.

This works:

$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'];
}

The output is:

Array ( [0] => example.com [3] => stackoverflow.com ) 

Upvotes: 1

Dino Babu
Dino Babu

Reputation: 5809

are you looking for this ?

$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);

Output

Array ( [0] => example.com [3] => stackoverflow.com ) 

Upvotes: 1

aaaaaa123456789
aaaaaa123456789

Reputation: 5842

There's no built-in way of finding "similar" records (mainly because there's many definitions for "similar"!). However, you can do it manually, by iterating over one of the array, and finding if there's a similar element in the other.

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

Where, of course, getBase() would be a function (that you'd have to define) that gives you the base parts of the strings, the parts you want to match (getBase('http://example.com/abc') should give you example.com). You could also use a callback.

Upvotes: 1

Dave
Dave

Reputation: 3288

You could probably use array_search() or array_uintersect() looping through one array and loose searching on the other array or uintersect with call back to strpos

Upvotes: 0

Related Questions