0

I ran into a problem trying to get a unique array from two two multidimensional array, failed to do so.

I've tried search on solutions, but everything I can thing is only about one array.

Here are the two arrays.

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
        )

)

I'm trying to make a function which compare url field from array 1 with domain field from array 2, and returns array 1 minus elements which are also found in array 2.

And even better function would also be if it could compare the field url in array 1 with domain field in array 2, and return with how much the two fields are matching.

3
  • What is the comparison field ? Commented Nov 15, 2015 at 14:47
  • 1
    Paste the content from pastie to your question. Click edit. Commented Nov 15, 2015 at 14:51
  • Edited. Sorry, through it would be to big if I added the arrays Commented Nov 15, 2015 at 15:23

1 Answer 1

3

You want to do this:

  1. make a lookup table where you can quickly check if a domain should be excluded in without having to search the entire array
  2. make a function that takes a url and returns its domain
  3. make a new array that inserts items that exist in array2 so long as those domains aren't in your lookup table of excluded domains.

Here's the code:

// 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;
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, Totally what I was looking for. Thank you very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.