Ich muss unique urls
aus einem Array identifizieren.PHP-Array einzigartig für URLs
http://google.com
https://google.com
http://www.google.com
https://www.google.com
www.google.com
google.com
Ich habe folgende Lösung:
public static function array_unique_url(array $array) : array
{
$uniqueArray = [];
foreach($array as $item) {
if(!self::in_array_url($item, $uniqueArray)){
$uniqueArray[] = $item;
}
}
return $uniqueArray;
}
public static function in_array_url(string $needle, array $haystack): bool {
$haystack = array_map([self::class, 'normalizeUrl'], $haystack);
$needle = self::normalizeUrl($needle);
return in_array($needle, $haystack);
}
public static function normalizeUrl(string $url) {
$url = strtolower($url);
return preg_replace('#^(https?://)?(www.)?#', '', $url);
}
Dies ist jedoch nicht sehr effizient O (n^2)
Alle folgenden Varianten sollten als gleich zählen. Kann mir jemand auf eine bessere Lösung hinweisen?
Sie Ihre Frage einmal erklären können? –
@SahilGulati Ich denke, es ist ziemlich klar. Und die Leute verstehen die Frage – Chris