note 52044 added to function.array-diff
| From: | rafmav at osu1 dot php dot net | Date: | Tue, 19 Apr 2005 23:24:09 +0000 |
| Subject: | note 52044 added to function.array-diff | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-88229@lists.php.net to get a copy of this message | ||
Here is a few functions to do a fast diff between two arrays in a few lines.
You can use it with other functions described in the function array_merge : array_merge_replace from
an other user, and two functions using it : array_merge_diff and array_merge_diff_reverse.
Note that the keys are preserved!
<?
// returns a two dimensions array with the deleted data
// and the added data
function array_diff_both($new,$old)
{
$del=array_diff_assoc($old,$new);
$add=array_diff_assoc($new,$old);
return $diff=array("del"=>$del, "add"=>$add);
}
// returns a two dimensions array with the equal data,
// deleted data and the added data
function array_diff_all($arr_new,$arr_old)
{
$arr_equ=array_intersect_assoc($arr_new,$arr_old);
$arr_del=array_diff_assoc($arr_old,$arr_new);
$arr_add=array_diff_assoc($arr_new,$arr_old);
return $diff=array("equ"=>$arr_equ, "del"=>$arr_del,
"add"=>$arr_add);
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.array-diff.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+52044
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52044&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52044&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52044&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52044&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52044&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+52044&report=yes
Search -- http://master.php.net/manage/user-notes.php