Comparison of array in php

Asked

Viewed 134 times

5

I have a array of the kind

array1(array(1,2,3,4,5), array(2,3,4,5,6));

and another of the kind

array2(1,2,3,4,6);

I need to compare array1 with array2 and pick the difference between them, but array1 is a set of arrays and I don’t know how to do this.

I used the function array_diff($array1, $array2); that when used with simple arrays as is the case with array2, returns the difference between them in an array, it would be great if it worked with each array of array1 compared to array2, but I read about the function and nothing talks about comparing these types of arrays.

  • you then need to generate an array of differences between array1 positions?

  • Exact, each position of the array1 be compared with the array2.

  • what difference you want to specify better, you want to know how many elements there are within one array and how many there are in the other?

  • vc wants to compare the array2 with the array1 or with the first array within the array1?

  • The difference of the elements, for example the first position of array1 has 5 and array2 does not, while the second position also has 5 and array2 does not. basically I want to learn how to compare each position of 1 with 2. the two is fixed, what complicates me is that array1 is an array with several other arrays.

2 answers

6


You can use the function array_map:

$array1 = [[1,2,3,4,5], [2,3,4,5,6]];
$array2 = [1,2,3,4,6];

$diferencas = array_map(function ($array1) use ($array2) {
    return array_diff($array1, $array2);
}, $array1);

The result will be:

Array
(
    [0] => Array
        (
            [4] => 5
        )

    [1] => Array
        (
            [3] => 5
        )

)

For the number 5 is the only element that is in $array1 and that is not in $array2.

If you do not need to keep the $array1 intact, you can use array_walk:

$array1 = [[1,2,3,4,5], [2,3,4,5,6]];
$array2 = [1,2,3,4,6];

array_walk($array1, function (&$array1, $index) use ($array2) {
  $array1 = array_diff($array1, $array2);
});

In which $array1 would be:

Array
(
    [0] => Array
        (
            [4] => 5
        )

    [1] => Array
        (
            [3] => 5
        )

)

Different from the first option, which creates a new array, this changes the array original by reference.

  • is exactly that! I always get confused if I can thank you here in the community, but anyway, thank you!

6

You need to pick up each position and check with array_diff what is the difference of the other array and create a new array with these differences, example:

<?php

    $array1 = array(array(1,2,3), array(3,4,5));
    $array2 = array(3,4);

    $arrayfinal = array();

    foreach($array1 as $arr)
    {
        $arrayfinal[] = array_diff($arr, $array2);
    }

    print_r($arrayfinal);

Exit:

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

another link: Verification of repeated information in array

Browser other questions tagged

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