Return equal value between two lists using the preg_match function in PHP

Asked

Viewed 29 times

1

Hello, I have two lists and would like to know how I can return the equal values between the elements of these two lists.

Code:

$lista1 = ["CURITIBA","SP","BH","RS"];
$lista2 = ["RJ","SC","AM","CURITIBA"];
$matches = array(); 

for($i=0;$i <= 3; $i++){

 $string = "$lista1[$i]";
 echo "Valor CMDB: $string"."</br>"; 
 $pattern = "/$lista2[$i]/";
 echo "Valor HUB: $pattern"."</br>";
 preg_match($pattern , $string, $matches);

 if(array_key_exists($i, $matches)){
    echo "Match: $matches[$i]"."</br>"."</br>"; 
 }else{
    echo "No Match"."</br>"."</br>"; 
 } 

}

Man if is always returning to me No Match, I believe it is because of the order of the elements, how can I make him identify the equal elements and return them to me in my echo?

View code online

  • The doubt was related to Function preg_match(), but the rray showed a clearer and objective form, the POST is valid for qm have future doubts about this function, in case I was unaware of the function array_intersect()

  • yes, the question I mentioned presents precisely the solution that rray posted here, because they are the same problems.

  • Got it, buddy, thank you all for your attention!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.