Compare arrays

Asked

Viewed 618 times

1

Good afternoon.

I would like to know how I can be comparing two arrays and keeping each pair of value found and print the numbers that were left without pair.

Ex: a) 1,2,3,4,5 b) 1,3,5,6,7,5,3

It would keep the pairs (1,1) (3,3) (5,5) and print the values 2,4,6,7,5,3 Note that 5 and 3 also have to be printed because not another that keeps the pair of the same.

I need to know exactly those values that have no pair, even if they are repeated. If anyone can shed some light, thank you.

Hugs

2 answers

2

PHP has a function called array_diff.

Basically what it does is what you want, the difference is that you will need to give a few more iterations, because it shows only the differences between two arrays, so in the very example given by the php site:

<?php
$array1 = array("a" => "verde", "vermelho", "azul", "vermelho");
$array2 = array("b" => "verde", "amarelo", "vermelho");
$result = array_diff($array1, $array2);
print_r($result);
?>

The result would be

Array
(
  [1] => azul
)

The opposite of this function would be what you could look for to make the first step, the array_intersect joins two arrays as one inner join and returns the values that exist in both:

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

Would return:

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

2


I believe that this code results in the problem, first a search is done in the array $b passing the current value of $a, case found is created a new element in $iguais who basically does the same thing array_diff() and this item is removed from $b not to enter the count again. Otherwise an item is added to $diferentes. At the end is made the sum of the items that exist in $a that do not exist in $b with that line: ($diferentes += $b;)

Example - ideone

<?php
$a = [1,2,3,4,5];
$b = [1,3,5,6,7,5,3];

$iguais = [];
$diferentes = [];
foreach($a as $item){
    $i = array_search($item, $b);
    if($i !== false){
        $iguais[] = '('. $item .', '. $b[$i] .')';
        unset($b[$i]);
    }else{
        $diferentes[] = $item;
    }
}

$diferentes += $b;

echo "<pre>";
print_r($iguais);

echo "<pre>";
print_r($diferentes);

Output:

Array
(
    [0] => (1, 1)
    [1] => (3, 3)
    [2] => (5, 5)
)

Array
(
    [0] => 2
    [1] => 4
    [3] => 6
    [4] => 7
    [5] => 5
    [6] => 3
)
  • Thanks rray, exactly what I wanted.

  • @Gustavofreire if solved your problem can mark the answer as accepted, can see in => How and why to accept an answer?

  • Just one more question, he printed the values not found , but printed together, would it be possible to print the amounts of $a that are not in $b and $b that are not in $a, that separately? Thank you

  • @Gustavofreire call twice array_diff() solves the problem? $diffab = array_diff($a, $b);&#xA;$diffba = array_diff($b, $a); see if this is it

  • Printed only the numbers other than $a, in which case 8 was missing in $b $a = [1,2,3,4,5,7,7,9]; $b = [2,4,7,8,9]; EQUAL - Array ( [0] => (2, 2) [1] => (4, 4) [2] => (7, 7) [3] => (9, 9) ) DIFFERENT - Array& DIFFERENT - Array( [0] => 1 [1] => 3 [2] => 5 [3] => 7 )

  • @Gustavofreire the difference from b to a was in $diffba

  • Yes, but can I print in two arrays the difference of each one? For example Array 1 = $diffab Array 2 = $diffba ? Because I need to take the numbers of each 1 add and then subtract Array 1 from Array 2.

Show 2 more comments

Browser other questions tagged

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