D
D
Dmitry2016-06-24 15:56:09
PHP
Dmitry, 2016-06-24 15:56:09

Compare two arrays containing guid?

Hello!
I have two arrays:

$arr1 = array( "45ebdbaa-380b-483b-80a2-73d7c53088e2","97baa061-4208-4aeb-8136-eb76c0932a3d");

$arr2 = array( "45ebdbaa-380b-483b-80a2-73d7c53088e2");

Detail array:
Array
(
    [0] => 090e3016-a593-11e4-8277-94de8067e7c4
    [1] => 090e3018-a593-11e4-8277-94de8067e7c4
    [2] => 090e301a-a593-11e4-8277-94de8067e7c4
    [3] => 090e301e-a593-11e4-8277-94de8067e7c4
)
второй массив:
Array
(
[22] => c7b8bdb2-a592-11e4-8277-94de8067e7c4
    [23] => c7b8bdb4-a592-11e4-8277-94de8067e7c4
    [24] => c7b8bdb8-a592-11e4-8277-94de8067e7c4
    [25] => c7b8bdba-a592-11e4-8277-94de8067e7c4
    [26] => c7b8bdc2-a592-11e4-8277-94de8067e7c4
    [27] => 090e3016-a593-11e4-8277-94de8067e7c4
    [28] => 090e3018-a593-11e4-8277-94de8067e7c4
)

It is required to select a discrepancy between arrays, but for some reason the array_diff function throws out the value, separately, in general, does not work correctly.
How to compare these arrays?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
entermix, 2016-06-24
@muldy

$arr1 = array( "45ebdbaa-380b-483b-80a2-73d7c53088e2","97baa061-4208-4aeb-8136-eb76c0932a3d");
$arr2 = array( "45ebdbaa-380b-483b-80a2-73d7c53088e2");
var_dump(array_diff($arr1, $arr2));

array (size=1)
  1 => string '97baa061-4208-4aeb-8136-eb76c0932a3d' (length=36)

$arr1 = array(
    0 => '090e3016-a593-11e4-8277-94de8067e7c4',
    1 => '090e3018-a593-11e4-8277-94de8067e7c4',
    2 => '090e301a-a593-11e4-8277-94de8067e7c4',
    3 => '090e301e-a593-11e4-8277-94de8067e7c4',
    );

$arr2 = array(
    22 => 'c7b8bdb2-a592-11e4-8277-94de8067e7c4',
    23 => 'c7b8bdb4-a592-11e4-8277-94de8067e7c4',
    24 => 'c7b8bdb8-a592-11e4-8277-94de8067e7c4',
    25 => 'c7b8bdba-a592-11e4-8277-94de8067e7c4',
    26 => 'c7b8bdc2-a592-11e4-8277-94de8067e7c4',
    27 => '090e3016-a593-11e4-8277-94de8067e7c4',
    28 => '090e3018-a593-11e4-8277-94de8067e7c4',
    );
var_dump(array_diff($arr1, $arr2));

array (size=2)
  2 => string '090e301a-a593-11e4-8277-94de8067e7c4' (length=36)
  3 => string '090e301e-a593-11e4-8277-94de8067e7c4' (length=36)

What does not suit?

V
Vitaliy Orlov, 2016-06-24
@orlov0562

array_diff is exactly the function you need:

<?php

$arr1 = array( "45ebdbaa-380b-483b-80a2-73d7c53088e2","97baa061-4208-4aeb-8136-eb76c0932a3d");

$arr2 = array( "45ebdbaa-380b-483b-80a2-73d7c53088e2");

print_r(array_diff($arr1, $arr2));

gives the result:
maybe the problem is somewhere else?

A
Andrey Burov, 2016-06-24
@BuriK666

$arr1 = array( "45ebdbaa-380b-483b-80a2-73d7c53088e2","97baa061-4208-4aeb-8136-eb76c0932a3d", "111");
$arr2 = array( "45ebdbaa-380b-483b-80a2-73d7c53088e2", "222");
$result = array_merge(array_diff($arr1, $arr2), array_diff($arr2, $arr1));
var_dump($result);
/*
array(3) {
  [0] =>
  string(36) "97baa061-4208-4aeb-8136-eb76c0932a3d"
  [1] =>
  string(3) "111"
  [2] =>
  string(3) "222"
}
*/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question