note 29607 deleted from function.array-merge-recursive by sniper
| From: | sniper@php.net | Date: | Mon, 06 Oct 2003 22:58:59 +0000 |
| Subject: | note 29607 deleted from function.array-merge-recursive by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-57935@lists.php.net to get a copy of this message | ||
Note Submitter: andrew __@T__ majiclab __D.T__ com
----
I created a function that does the mix between array_merge and array_merge_recursive. I call it
array_merge_recursive_unique(). It will not duplicate any items or add keys. It uses a function
amru_core which does most of the dirty work. The main function can take any number of arrays and
will merge all of them. Have fun with this one. (View source to copy a nicer looking version)
function amru_core( &$retrn, $cArray )
{
foreach( $cArray as $index => $sArray )
{
if( is_array( $sArray ) )
{
if( !isset( $retrn[ $index ] ) )
$retrn[ $index ] = array();
amru_core( $retrn[ $index ], $sArray );
}
else
{
if( !isset( $retrn[ $index ] ) )
$retrn[ $index ] = $sArray;
}
}
}
function array_merge_recursive_unique()
{
$retrn = array();
$rArray = array();
$pArray = func_get_args();
foreach( $pArray as $Arr )
{
if( is_array( $Arr ) )
$rArray[] = $Arr;
}
foreach( $rArray as $tArray )
amru_core( $retrn, $tArray );
return $retrn;
}