Reputation: 4505
I have got a large array of lets say 500 urls, now using array_unique
I can remove any duplicate values. However I am wanting to remove any duplicate values where the domain is the same while keeping the original domain (so only removing duplicates so this value is now unique).
I have been using the following however this only removes duplicate values:
$directurls = array_unique($directurls);
I have been playing around with the following to get the domain but am wondering how I can check the entire array for other parse_url
domains in the array:
foreach($directurls as $url) {
$parse = parse_url($url);
print $parse['host']; //the domain name I just need to find a way to check this and remove it
}
I imagine I will need to use some form of loop perhaps where I can get the current host and check all other hosts in the array. If duplicates remove all duplicates and keep the current value. Maybe something like this could work am just testing it now:
foreach($directurls as $url) {
$parse = parse_url($url);
if (in_array($parse['host'], $directurls)) {
//just looking for a way to remove while keeping unique
}
}
If anyone has any suggestions or recommendations on other ways to go about this I would be very thankful.
Let me know if I need to explain a bit more.
Upvotes: 2
Views: 771
Reputation: 76646
You could avoid having to loop through the URLs by using array_map()
with a callback function.Grab the domain using parse_url()
, and create a new array with just the domains. Now, you can simply create a new array with the URLs as keys and domains as values and just call array_unique()
to get the unique items. Now, to get just the URLs into a new array, you can use array_keys()
:
$domains = array_map(function($d) {
$parts = parse_url($d); // or: parse_url($d)['host'] if PHP > 5.4
return $parts['host'];
}, $directurls);
$result = array_keys(array_unique(array_combine($directurls, $domains)));
Upvotes: 2