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

4

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

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)

Now the query of the 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 find out which users are in one and not another, to bring the difference between the arrays ...

To save the values of each one I did so

mysql = $lista1 = mysqli_fetch_all($result);

sqlServer = $lista2 = $consulta->fetchAll();  (pq uso PDO);
    
asked by anonymous 26.01.2016 / 12:43

2 answers

5

This function does a better job, because it performs an 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: link

    
26.01.2016 / 13:13
2

If you just want to return a single array , both with users that are array and another, it is easier for you to join the two arrays and then return the unique values of sum two two.

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

Consider this example an attachment exercise:

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

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

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

The result of joining these arrays, generate 6 items, but 5 are returned, since duplicate values are transformed into unique.

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

Try Ideone

    
26.01.2016 / 13:19