How to compare values ​​in two arrays?

It seems that every PHP function that I read to compare arrays ( array_diff() , array_intersect() , etc.) is compared for the existence of array elements.

Given two multidimensional arrays with the same structure, how would you indicate differences in values ?

Example

Array 1

  [User1] => Array ([public] => 1
                 [private] => 1
                 [secret] => 1
                ) 
 [User2] => Array ([public] => 1
                 [private] => 0
                 [secret] => 0
                )

Array 2

  [User1] => Array ([public] => 1
                 [private] => 0
                 [secret] => 1
                ) 
 [User2] => Array ([public] => 1
                 [private] => 0
                 [secret] => 0
                )

Difference

  [User1] => Array ([public] => 1
                 [private] => 0 // this value is different
                 [secret] => 1
                )

Thus, my result will be: "Of all users, User1 has changed, and the difference is that private is 0 instead of 1".

+4
source share
4 answers

One way is to write a function to do something similar to this.

 function compareArray ($array1, $array2) { foreach ($array1 as $key => $value) { if ($array2[$key] != $value) { return false; } } return true; } 

You can easily increase this function to return an array of differences in two.

Edit - here is an improved version that looks more like what you are looking for:

 function compareArray ($array1, $array2) { var $differences; foreach ($array1 as $key => $value) { if ($array2[$key] != $value) { $differences[$key][1] = $value; $differences[$key][2] = $array2[$key]; } } if (sizeof($differences) > 0) { return $differences; } else { return true; } } 
+5
source

I think this does what you are looking for.

Using your sample data, loop through the external arrays, then using array_diff_assoc for users each time. (Note that this assumes that when there is a difference, array_diff_assoc returns the value from the second passed array, which seems to do).

 <?php $user1 = array("public" => 1, "private" => 1, "secret" => 1); $user2 = array("public" => 1, "private" =>1, "secret" => 1); $array1 = array ("user 1"=>$user1, "user 2"=>$user2); $user1 = array("public" => 1, "private" => 0, "secret" => 1); $user2 = array("public" => 1, "private" => 1, "secret" => 1); $array2 = array("user 1"=>$user1, "user 2"=>$user2); $results = array(); foreach ( $array1 as $user => $value ) { $diff = array_diff_assoc( $array1[$user], $array2[$user] ); if ($diff) { array_push($results,array($user=>$diff)); } } print_r($results); ?> 

It returns:

 Array ( [0] => Array ( [user 1] => Array ( [private] => 1 ) ) ) 
+1
source

Try this feature:

 function arrayDiff($array1, $array2) { if (!is_array($array1) || !is_array($array2)) { return false; } foreach ($array1 as $key => $val) { if (array_key_exists($key, $array2) && gettype($val) != "array" && $val === $array2[$key]) { unset($array1[$key]); continue; } if (is_array($val)) { $val = diff($val, $array2[$key]); if ($val !== false) { $array1[$key] = $val; } } } return $array1; } $array1 = array( array( array('foo', 'bar', 'baz'), 0 ) ); $array2 = array( array( array('foo', 'bar') ) ); var_dump(diff($array1, $array2)); 
0
source

If you are looking for differences in values, what about array_diff_assoc . Http://us2.php.net/manual/en/function.array-diff-assoc.php "> php manual says that this" returns an array containing all the values from array1 that are not present in any of the other arrays ", and gives the following example:

In this example, you see "a" => a "green" pair is present in both arrays and, therefore, it is not in a function. In contrast, the pair 0 => "red" is in the output, because in the second argument, "red" has a key that is 1

 <?php $array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red"); $array2 = array("a" => "green", "yellow", "red"); $result = array_diff_assoc($array1, $array2); print_r($result); ?> 

The above example outputs:

 Array ( [b] => brown [c] => blue [0] => red ) 

Is this what you are looking for?

0
source

All Articles