php - Compare arrays of urls and remove from one array urls depend on second array -


i need 1 think should do. have 2 arrays urls example:

$urls = ['https://test.com/', 'http://example.com/', 'https://google.com/'];  $urlsfromothersource = ['https://test.com/', 'https://example.com/', 'https://facebook.com/']; 

i need create 3 arrays of urls there. first of them have common urls both arrays. 2 others same if in 2 initials array have same url difference in http - https need assignet url 1 array.

so example 2 arrays need arrays in following way:

 $commonurls = ['https://test.com/']; //becouse have url in 2 arrays    $urls = ['http://example.com/', 'https://google.com/'];   //'http://example.com/ leave in array url , remove second table becouse in second array have same- difference in https     $urlsfromothersource = ['https://facebook.com/']; //remove array https://example.com/ becouse url in first array- difference in http 

i tried think how can compare arrays , catch difference in http-https not easy me. code this:

  $urls = ['https://test.com/', 'http://example.com/', 'https://google.com/'];  $urlsfromothersource = ['https://test.com/', 'https://example.com/', 'https://facebook.com/'];          $commonurls = array_intersect($urls, $urlsfromothersource);//here have common urls both arrays         $urls = array_diff($urls, $commonurls);//i remove array urls have in common array         $urlsfromothersource = array_diff($urlsfromothersource, $commonurls);//i remove array urls have in common array           foreach ($urlsfromothersource $url) {             $landingpagearray[] = preg_replace(["#^http(s)?://#", "#^www\.#"], ["", ""], $url);         }          foreach ($urls $url) {             $landingpage = preg_replace(["#^http(s)?://#", "#^www\.#"], ["", ""], $url);             if (in_array($landingpage, $landingpagearray)) {                 $httpdifference[] = $url;             }         }         //i havent idea how can remove $urlsfromothersource urls have in $urls array , difference in http-https         $urlsfromothersource = array_diff($urlsfromothersource, $httpdifference); 

so need compare arrays , remove second array urls have in first array , difference between url http-htpps. maybe can me find algorithm that.

update need remove urlsfromothersource if have url in commonurls:

commonurls: array(1) {   [0]=>   string(17) "http://www.test.com/" }    urlsfromothersource: array(1) {   [2]=>   string(21) "http://test.com/" } 

so need remove urlsfromothersource url. , make code automatically compare landing page whatever http://www or www or http:// need not compare in arrays

you can write own comparison function using u-methods, array_udiff , array_uintersect. use preg_replace when comparing urls ignore difference http/https.

$commonurls = array_intersect($urls, $urlsfromothersource);//here have common urls both arrays  $urls = array_diff($urls, $commonurls);  $urlsfromothersource = array_udiff(array_diff($urlsfromothersource, $commonurls), $urls, function ($a, $b) {   return strcmp(preg_replace('|^https?://(www\\.)?|', '', $a), preg_replace('|^https?://(www\\.)?|', '', $b)); }); 

this yields:

commonurls: array(1) {   [0]=>   string(17) "https://test.com/" }  urls: array(2) {   [1]=>   string(19) "http://example.com/"   [2]=>   string(19) "https://google.com/" }  urlsfromothersource: array(1) {   [2]=>   string(21) "https://facebook.com/" } 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -