note 52048 added to function.array-merge

From: Date: Tue, 19 Apr 2005 23:54:21 +0000
Subject: note 52048 added to function.array-merge
Groups: php.notes 
Request: Send a blank email to php-notes+get-88233@lists.php.net to get a copy of this message
Here are a few functions to make a diff between two arrays (or 2 strings, see the example) and merge it back after <? 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); } 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); } function array_merge_diff($arr,$diff) { $arr=array_merge_replace($arr,$diff["del"]); $arr=array_merge_replace($arr,$diff["add"]); return $arr; } function array_merge_diff_reverse($arr,$diff) { $arr=array_merge_replace($arr,$diff["add"]); $arr=array_merge_replace($arr,$diff["del"]); return $arr; } ?> Here is an example: <? //calculationg $new="hello!"; // new string $old="hrllo!"; // old string with a typo fault // cast string in array the goodway // make a cast (array) to a string does not work: // the entire string is thus considered as one element // of the array !! $a_new=str_split($new); $a_old=str_split($old); // cast string in array $diff=array_diff_both($a_new,$a_old); // the entire diff $test=array_merge_diff($a_old,$diff); $test_reverse=array_merge_diff_reverse($a_new,$diff); //for output the sample print "new= $new<br/>old= $old<br/>\$a_new= "; print_r($a_new); print "<br/>\$a_old= "; print_r($a_old); print "<br/>\$diff=\$a_new-\$a_old"; print "<br/>\$diff= "; print_r($diff); print "<br/>\$test=\$a_old+\$diff"; print "<br/>\$test= "; print_r($test); print("<br/>".implode($test)); print "<br/>\$test_reverse=\$a_new+\$diff"; print "<br/>\$test_reverse= "; print_r($test_reverse); print("<br/>".implode($test_reverse)); ?> En sortie de l'exemple, résultats: new= hello! old= hrllo! $a_new= Array ( [0] => h [1] => e [2] => l [3] => l [4] => o [5] => ! ) $a_old= Array ( [0] => h [1] => r [2] => l [3] => l [4] => o [5] => ! ) $diff=$a_new-$a_old $diff= Array ( [del] => Array ( [1] => r ) [add] => Array ( [1] => e ) ) $test=$a_old+$diff $test= Array ( [0] => h [1] => e [2] => l [3] => l [4] => o [5] => ! ) hello! $test_reverse=$a_new+$diff $test_reverse= Array ( [0] => h [1] => r [2] => l [3] => l [4] => o [5] => ! ) hrllo! autre exemple, autre résultat: new= see you! old= sea ya! $a_new= Array ( [0] => s [1] => e [2] => e [3] => [4] => y [5] => o [6] => u [7] => ! ) $a_old= Array ( [0] => s [1] => e [2] => a [3] => [4] => y [5] => a [6] => ! ) $diff=$a_new-$a_old $diff= Array ( [del] => Array ( [2] => a [5] => a [6] => ! ) [add] => Array ( [2] => e [5] => o [6] => u [7] => ! ) ) $test=$a_old+$diff $test= Array ( [0] => s [1] => e [2] => e [3] => [4] => y [5] => o [6] => u [7] => ! ) see you! $test_reverse=$a_new+$diff $test_reverse= Array ( [0] => s [1] => e [2] => a [3] => [4] => y [5] => a [6] => ! [7] => ! ) sea ya!! ---- Manual Page -- http://www.php.net/manual/en/function.array-merge.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+52048 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52048&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52048&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52048&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52048&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52048&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+52048&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#88233) next »