Check for repeated numbers between two php arrays

Asked

Viewed 812 times

2

Ola, I have two arrays in php and I need to check if there is a number in array 1 that repeats with some number in array 2. I don’t need the numbers, I just need to know if it repeats! How to do?

2 answers

0

You can use the array_intersect

<?php

   $array1 = array("a" => "verde", "vermelho", "azul");
   $array2 = array("b" => "verde", "amarelo", "vermelho");
   $result = array_intersect($array1, $array2);
   print_r($result);

?>

Return:

Array
(
    [a] => verde
    [0] => vermelho
)

0


Using the function array_intersect of php:

<?php
$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r($result);
?>

In this case, it will return everything contained in the two arrays keeping their chaves:

Array
(
    [a] => green
    [0] => red
)

You can use or create another function that uses this measure as is best for your rule.

  • Perfect guy. That’s what it was!!! Vlw!!!

Browser other questions tagged

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