Check if an element of an array is contained in each element of another array with php

3

I have two arrays, I need to get each element of the second array and check if it is contained in any element of the first array using, for example, the function strpos ().

That is, I need to check if any string from list 2 is contained in some URL from list 1. Thank you in advance.

<?php
$ads = array();
$ads[0] = "https://superman.com.br";
$ads[1] = "https://photoshop.com.br?galid=";
$ads[2] = "https://mercado.com.br";
$ads[3] = "https://xdemais.com.br";
$ads[4] = "https://imagens.com.br";
$ads[5] = "https://terceiraidade.com.br";
$ads[6] = "https://goldenartesgraficas.com.br";
$ads[7] = "https://empregos.com.br";
$ads[8] = "https://umcentavo.com.br";
$ads[9] = "https://classificados.com.br";


	$filter_ads = array();
	$filter_ads[0] = "galid=";
	$filter_ads[1] = "gslid=";
	$filter_ads[2] = "ghlid=";
	$filter_ads[3] = "gplid=";
	$filter_ads[4] = "gulid=";
	$filter_ads[5] = "gllid=";
	$filter_ads[6] = "gklid=";
	$filter_ads[7] = "grlid=";
	$filter_ads[8] = "gwlid=";
	$filter_ads[9] = "gelid=";	

foreach($ads as $ads_x) {
    if (strpos($ads_x, in_array($filter_ads, $ads))) {
    	echo "Existe";
    }
}	
?>
    
asked by anonymous 01.02.2016 / 03:21

1 answer

2

Scroll through the 2 arrays for an instance:

foreach ($ads as $link) {

    foreach ($filter_ads as $ads) {

        if (strpos($link, $ads)){
            echo "Achei aqui: <br />"
            . "Link: ". $link ." <br />"
            . "parametro: ". $ads ."<br /><br />";
        }

    }

}

You can still test these other 2 options depending on your need they can solve. In the 2 cases I made the array of links in a large comma-separated string just to look for a filter instance:

So it will return 1 if it finds and 0 if it does not find:

foreach ($filter_ads as $filter) {
    echo preg_match("/". $filter ."/", implode(",", $ads));
}

So it will return the position of the instance, if it occurs:

foreach ($filter_ads as $filter) {
    echo strpos(implode(",", $ads), $filter);
}

I hope I have helped!

    
01.02.2016 / 12:45