2012-04-09 28 views
25

Tengo dos matrices siguientes. Quiero la diferencia entre estas dos matrices. Es decir, ¿cómo puedo encontrar los valores que no existen en ambas matrices?diferencia entre dos matrices

$array1=Array ([0] => 64 [1] => 98 [2] => 112 [3] => 92 [4] => 92 [5] => 92) ; 
$array2=Array ([0] => 3 [1] => 26 [2] => 38 [3] => 40 [4] => 44 [5] => 46 [6] => 48 [7] => 52 [8] => 64 [9] => 68 [10] => 70 [11] => 72 [12] => 102 [13] => 104 [14] => 106 [15] => 92 [16] => 94 [17] => 96 [18] => 98 [19] => 100 [20] => 108 [21] => 110 [22] => 112); 

Respuesta

25

Nota: esta respuesta se devuelve los valores en $ matriz2 que no están presentes en $ matriz1, no devolverá los valores en $ matriz1 que no están en $ matriz2.

$diff = array_diff($array2, $array1); 

array_diff()

+12

Esto parece ser incorrecto; no devolverá valores de '$ array2' que no existan en' $ array1'. [Crasherspeeder] (http://stackoverflow.com/questions/10077840/difference-between-two-arrays/10077920#10077920) parece tenerlo correcto. – Shog9

+7

@Crasherspeeder es correcto, esta es una comprobación unidireccional: el solicitante quiere los dos. –

118

Para obtener la diferencia entre las dos matrices que hay que hacer lo siguiente:

$fullDiff = array_merge(array_diff($array1, $array2), array_diff($array2, $array1)); 

La razón es que array_diff() sólo le dará los valores que están en $array1 pero no $array2, no al revés. Lo anterior te dará ambos.

1
<?php 
function getArrayDiff($a1, $a2) { 
    $result = array(); 

    print_r($a1); 
    print_r($a2); 

    // If First Array is Bigger than Second 
    if(count($a1) > count($a2)) { 
     $result=array_diff($a1,$a2); 
    } 
    // If Second Array is Bigger than First 
    if(count($a1) < count($a2)) { 
     $result=array_diff($a2,$a1); 
    } 
    // If Both array are same but, data values are different. 
    else 
    { 
     $result = array_merge (array_diff($a2,$a1), array_diff($a1,$a2)); 
    } 
    return $result; 
} 

print "<pre>"; 
// First Array is Big 
echo "First Array is Big <br/>"; 
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow"); 
$a2=array("e"=>"red","f"=>"green","g"=>"blue"); 
print_r(getArrayDiff($a1, $a2)); 

// Second Array is Big 
echo "Second Array is Big <br/>"; 
$a1=array("e"=>"red","f"=>"green","g"=>"blue"); 
$a2=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow"); 
print_r(getArrayDiff($a1, $a2)); 

// Both Array are same 
echo "Both Array are same <br/>"; 
$a1=array("a"=>"red","b"=>"green","d"=>"yellow"); 
$a2=array("e"=>"red","f"=>"green","g"=>"blue"); 
print_r(getArrayDiff($a1, $a2)); 

?> 

Salida:

First Array is Big 
Array 
(
    [a] => red 
    [b] => green 
    [c] => blue 
    [d] => yellow 
) 
Array 
(
    [e] => red 
    [f] => green 
    [g] => blue 
) 
Array 
(
    [d] => yellow 
) 
Second Array is Big 
Array 
(
    [e] => red 
    [f] => green 
    [g] => blue 
) 
Array 
(
    [a] => red 
    [b] => green 
    [c] => blue 
    [d] => yellow 
) 
Array 
(
    [d] => yellow 
) 
Both Array are same 
Array 
(
    [a] => red 
    [b] => green 
    [d] => yellow 
) 
Array 
(
    [e] => red 
    [f] => green 
    [g] => blue 
) 
Array 
(
    [g] => blue 
    [d] => yellow 
) 
5

Si desea obtener diferencia entre las matrices de forma recursiva, pruebe esta función:

function arrayDiffRecursive($firstArray, $secondArray, $reverseKey = false) 
{ 
    $oldKey = 'old'; 
    $newKey = 'new'; 
    if ($reverseKey) { 
     $oldKey = 'new'; 
     $newKey = 'old'; 
    } 
    $difference = []; 
    foreach ($firstArray as $firstKey => $firstValue) { 
     if (is_array($firstValue)) { 
      if (!array_key_exists($firstKey, $secondArray) || !is_array($secondArray[$firstKey])) { 
       $difference[$oldKey][$firstKey] = $firstValue; 
       $difference[$newKey][$firstKey] = ''; 
      } else { 
       $newDiff = arrayDiffRecursive($firstValue, $secondArray[$firstKey], $reverseKey); 
       if (!empty($newDiff)) { 
        $difference[$oldKey][$firstKey] = $newDiff[$oldKey]; 
        $difference[$newKey][$firstKey] = $newDiff[$newKey]; 
       } 
      } 
     } else { 
      if (!array_key_exists($firstKey, $secondArray) || $secondArray[$firstKey] != $firstValue) { 
       $difference[$oldKey][$firstKey] = $firstValue; 
       $difference[$newKey][$firstKey] = $secondArray[$firstKey]; 
      } 
     } 
    } 
    return $difference; 
} 

prueba:

$differences = array_replace_recursive(
    arrayDiffRecursive($firstArray, $secondArray), 
    arrayDiffRecursive($secondArray, $firstArray, true) 
); 
var_dump($differences); 
+0

mejor diferencia de matriz recursiva para php en la web – apfz

Cuestiones relacionadas