note 88707 added to function.array-merge-recursive

From: Date: Wed, 04 Feb 2009 11:56:26 +0000
Subject: note 88707 added to function.array-merge-recursive
Groups: php.notes 
Request: Send a blank email to php-notes+get-150198@lists.php.net to get a copy of this message
I would merge 2 arrays but keep the values unique in the result array. I hope this will help... <?php function array_merge_recursive_unique($array1, $array2) { foreach($array2 AS $k => $v) { if(!isset($array1[$k])) { $array1[$k] = $v; } else { if(!is_array($v)){ if(is_array($array1[$k])) { if(!in_array($v,$array1[$k])) { $array1[$k][] = $v; } } else { if($array1[$k] != $v) $array1[$k] = array($array1[$k], $v); } } else { $array1[$k] = array_merge_recursive_unique($array1,$array2[$k]); } } } unset($k, $v); return $array1; } ?> ---- Server IP: 213.246.51.26 Probable Submitter: 193.51.91.79 ---- Manual Page -- http://www.php.net/manual/en/function.array-merge-recursive.php Edit -- https://master.php.net/note/edit/88707 Del: integrated -- https://master.php.net/note/delete/88707/integrated Del: useless -- https://master.php.net/note/delete/88707/useless Del: bad code -- https://master.php.net/note/delete/88707/bad+code Del: spam -- https://master.php.net/note/delete/88707/spam Del: non-english -- https://master.php.net/note/delete/88707/non-english Del: in docs -- https://master.php.net/note/delete/88707/in+docs Del: other reasons-- https://master.php.net/note/delete/88707 Reject -- https://master.php.net/note/reject/88707 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#150198) next »