What I'm trying to do is take items from one array, such as:
$array1 = array(
"google-com",
"youtube-com",
);
And remove items from a second array if the above items are included (but BROAD match, not exact).
$array2 = array(
"www-google-com",
"www-youtube-com",
"www-facebook-com",
"www-twitter-com",
);
Expected output:
www-facebook-com
www-twitter-com
Note: The first array would be with "example.com" style URLs and the second with "https://www.example.com/" URLs.
It seems array_diff only works with exact matches, and after much searching, I can't seem to find a way to make it work for broad matches.
Thanks for your help!
You're right, array_diff
is not a solution here. One of the solutions is using a preg_grep
to find records and then unset keys in $array2
:
$array1 = array(
"google-com",
"youtube-com",
);
$array2 = array(
"www-google-com",
"www-youtube-com",
"www-facebook-com",
"www-twitter-com",
);
foreach ($array1 as $search) {
foreach (preg_grep('/' . $search . '/', $array2) as $index => $value) {
unset($array2[$index]);
}
}
print_r($array2);