note 116269 added to function.array-diff
| From: | wrey75 at gmail dot com | Date: | Wed, 03 Dec 2014 22:27:02 +0000 |
| Subject: | note 116269 added to function.array-diff | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-201061@lists.php.net to get a copy of this message | ||
The difference is made only on the first level. If you want compare 2 arrays, you can use the code
available at https://gist.github.com/wrey75/c631f6fe9c975354aec7
(including a class with an function to patch the array)
Here the basic function:
function my_array_diff($arr1, $arr2) {
$diff = array();
// Check the similarities
foreach( $arr1 as $k1=>$v1 ){
if( isset( $arr2[$k1]) ){
$v2 = $arr2[$k1];
if( is_array($v1) && is_array($v2) ){
// 2 arrays: just go further...
// .. and explain it's an update!
$changes = self::diff($v1, $v2);
if( count($changes) > 0 ){
// If we have no change, simply ignore
$diff[$k1] = array('upd' => $changes);
}
unset($arr2[$k1]); // don't forget
}
else if( $v2 === $v1 ){
// unset the value on the second array
// for the "surplus"
unset( $arr2[$k1] );
}
else {
// Don't mind if arrays or not.
$diff[$k1] = array( 'old' => $v1, 'new'=>$v2 );
unset( $arr2[$k1] );
}
}
else {
// remove information
$diff[$k1] = array( 'old' => $v1 );
}
}
// Now, check for new stuff in $arr2
reset( $arr2 ); // Don't argue it's unnecessary (even I believe you)
foreach( $arr2 as $k=>$v ){
// OK, it is quite stupid my friend
$diff[$k] = array( 'new' => $v );
}
return $diff;
}
----
Server IP: 72.52.91.14
Probable Submitter: 37.160.156.51
----
Manual Page -- http://php.net/manual/en/function.array-diff.php
Edit -- https://master.php.net/note/edit/116269
Del: integrated -- https://master.php.net/note/delete/116269/integrated
Del: useless -- https://master.php.net/note/delete/116269/useless
Del: bad code -- https://master.php.net/note/delete/116269/bad+code
Del: spam -- https://master.php.net/note/delete/116269/spam
Del: non-english -- https://master.php.net/note/delete/116269/non-english
Del: in docs -- https://master.php.net/note/delete/116269/in+docs
Del: other reasons-- https://master.php.net/note/delete/116269
Reject -- https://master.php.net/note/reject/116269
Search -- https://master.php.net/manage/user-notes.php