Percorer 2 Distinct Arrays and Verify Which Values are Equal

Asked

Viewed 103 times

0

How to go through 2 distinct arrays to verify which values are equal and after the check print only the equal values of both arrays?

The arrays' structures are in the following form below, I tried to traverse using Function in_array but it doesn’t work, someone can help?

Array 1

Array
(
    [0] => Array
        (
            [id] => 1899
            [bola1] => 1
            [bola2] => 3
            [bola3] => 5
            [bola4] => 6
            [bola5] => 9
            [bola6] => 10
            [bola7] => 11
            [bola8] => 14
            [bola9] => 15
            [bola10] => 16
            [bola11] => 17
            [bola12] => 18
            [bola13] => 19
            [bola14] => 20
            [bola15] => 23
        )

)

Array 2

Array
(
    [0] => Array
        (
            [id] => 1
            [bola1] => 1
            [bola2] => 2
            [bola3] => 3
            [bola4] => 4
            [bola5] => 6
            [bola6] => 7
            [bola7] => 8
            [bola8] => 10
            [bola9] => 11
            [bola10] => 13
            [bola11] => 14
            [bola12] => 18
            [bola13] => 20
            [bola14] => 21
            [bola15] => 24
        )

)
  • Does the key and value have to be equal? Only the value? Only the key?

  • Only the value, for example: the key value [ball1] of Array 1 is 1, the key value [ball1] of Array 2 is 1 so they are equal, prints 1. Already the key value [ball2] of Array 1 is 3, the key value [ball2] of Array 2 is 2, so they are not equal, captured the idea?

  • See if the array_intersect serves https://www.php.net/manual/en/function.array-intersect.php.

  • It didn’t work with this array_intersect function

2 answers

0


In your case the array is inside another array, try this.

$result = array_intersect($array1[0], $array2[0]);

print_r($result);

0

You’ve tried it this way ?

$arrNumber = [1,2,3,4,5]; $otherArr = [1,3,7,9];

foreach($arrNumber as $arr){ foreach ($otherArr as $v) { if($arr === $v){ print 'Equal values:'. $v ."
"; } } }

if it is an array within another array it traverses the first level and then the second one comparing the values.

Browser other questions tagged

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