How to compare two arrays and return the difference between them?

Asked

Viewed 4,557 times

4

I have 2 arrays that come from 2 different banks, one comes from sql server other mysql.

The Mysl query returns like this..

    array (size=6)
  0 => string 'ultimoteste3' (length=12)
  1 => string 'ultimoteste3' (length=12)
  2 => string 'Rodrigues' (length=9)
  3 => string '[email protected]' (length=24)
  4 => string '2' (length=1)
  5 => string 'Teste Curso' (length=11)

Already the query of sql server that is a Procedure returns like this:

 array (size=16)
  'username' => string 'username' (length=8)
  0 => string 'username' (length=8)
  'firstname' => string 'firstname' (length=9)
  1 => string 'firstname' (length=9)
  'lastname' => string 'lastname' (length=8)
  2 => string 'lastname' (length=8)
  'email' => string 'email' (length=5)
  3 => string 'email' (length=5)
  'lang' => string 'lang' (length=4)
  4 => string 'lang' (length=4)
  'course1' => string 'course1' (length=7)
  5 => string 'course1' (length=7)
  'type1' => string 'type1' (length=5)
  6 => string 'type1' (length=5)
  'auth' => string 'auth' (length=4)
  7 => string 'auth' (length=4)

How much I use

print_r(array_diff($lista1, $lista2));

the result comes like this...

Array ( )

I need to go through the two arrays and discover the users that are in one and not in the other, bring the difference between the arrays...

to keep the values of each one I did so

mysql = $lista1 = mysqli_fetch_all($result);

sqlServer = $lista2 = $consulta->fetchAll();  (pq uso PDO);
  • What is your question? Specify better, make it clear what you want help with.

  • How you store the result in $lista1 and $Lista2?

  • How do you expect it to return?

  • I hope that return different users between the arrays, to form another list of users, so I manipulate...

  • This second array ($Lista2) has a confusing structure, because it has a different index than $lista1?

  • Wow! Do you want to know if the user is unique?! It looks like he’s going down a tough road. But I get it, it’s two different banks.

  • 2

    i realized the test on ideone only with arrays and the result was "normal", it shows all array 1 because no data of it contains in array 2.

  • Make sure that the PHP version is higher than 4.0.4, according to the documentation the array_diff function has problems in the old versions.

  • Remember that the diff array displays the array values of the first parameter that are NOT in the second parameter. Not the other way around.

  • Make the following trade var_dump() for var_export($var, true); ai you put the ready estruta of the test arrays in the question.

Show 5 more comments

2 answers

5


This function does a better job as it performs a XOR operation on both arrays, the native PHP function only checks the values of the first parameter that are NOT in the second parameter:

function ary_diff( $ary_1, $ary_2 ) {
  // compare the value of 2 array
  // get differences that in ary_1 but not in ary_2
  // get difference that in ary_2 but not in ary_1
  // return the unique difference between value of 2 array
  $diff = array();

  // get differences that in ary_1 but not in ary_2
  foreach ( $ary_1 as $v1 ) {
    $flag = 0;
    foreach ( $ary_2 as $v2 ) {
      $flag |= ( $v1 == $v2 );
      if ( $flag ) break;
    }
    if ( !$flag ) array_push( $diff, $v1 );
  }

  // get difference that in ary_2 but not in ary_1
  foreach ( $ary_2 as $v2 ) {
    $flag = 0;
    foreach ( $ary_1 as $v1 ) {
      $flag |= ( $v1 == $v2 );
      if ( $flag ) break;
    }
    if ( !$flag && !in_array( $v2, $diff ) ) array_push( $diff, $v2 );
  }

  return $diff;
}

Source: http://php.net/manual/en/function.array-diff.php#76391

  • It would not be easier to join the two and then return the unique values (not to repeat)?

  • Walker, it worked, but is doing a MERGE, joining the two list.

  • @Yurirodrigues This is happening because both arrays are different from each other. You must work on the structure of arrays so that they have the same contents.

2

If you just want to return one array single, both with users who is with array as in another, it is easier for you to join the two arrays and then return the single values of the sum two.

$allUsersDiff = array_unique(
    array_merge($lista1, $lista2)
);

Consider this example a fixation exercise:

$array1 = array('wallace', 'wayne', 'marcos');

$array2 = array('cleber', 'wallace', 'junior');

var_dump(array_unique(array_merge($array1, $array2)));

The result of the union of these arrays, generate 6 items, but 5 are returned, because duplicated values are transformed into single.

array(5) {
  [0]=>
  string(7) "wallace"
  [1]=>
  string(5) "wayne"
  [2]=>
  string(6) "marcos"
  [3]=>
  string(6) "cleber"
  [5]=>
  string(6) "junior"
}

Testing at Ideone

Browser other questions tagged

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