I have this array which provides Geo-restriction information from API, this is an array list of countries where content is being BLOCKED :
Array ( [0] => GU [1] => PR [2] => CA [3] => VI [4] => US [5] => UM [6] => AS [7] => MP [8] => DE )
Now, I have another array which stores country-wise proxy information like this :
$proxies['US'] = 'my_us_proxy_url;
$proxies['DE'] = 'my_de_proxy_url;
$proxies['UK'] = 'my_uk_proxy_url;
$proxies['NL'] = 'my_nl_proxy_url;
I want to get value of the proxy which will allow user to bypass the country restriction i.e. a $proxies[KEY]
value where KEY
does not exist in the first array.
This is one of the snippet I tried but like everything else this is not the logic what is needed.
$isBlocked = array_values;
//print_r($isBlocked);
if (in_array('US',$isBlocked))
{
echo 'US Blocked';
foreach ($isBlocked as $value) {
if (!array_key_exists($value,$proxies)){
//Find first non blocked proxy and continue
echo "<br/>" . $value ;
}
}
}
Use $key => $value
structure in your foreach
loop:
foreach ($proxies as $key => $value) {
if (!in_array($key, $isBlocked)){
//Find first non blocked proxy and continue
echo "<br/>" . $value ;
}
}
Get the key from the proxies array via array_keys. Get the difference to the other array via array_diff. With large arrays that might a tad less expensive than foreach-loops.
That will be:
$data = ['GU', 'PR', 'CA', 'VI', 'US', 'UM', 'AS', 'MP', 'DE '];
$proxies['US'] = 'my_us_proxy_url';
$proxies['DE'] = 'my_de_proxy_url';
$proxies['UK'] = 'my_uk_proxy_url';
$proxies['NL'] = 'my_nl_proxy_url';
$result = array_diff_key($proxies, array_flip($data));