note 29607 added to function.array-merge-recursive

From: Date: Tue, 18 Feb 2003 23:52:31 +0000
Subject: note 29607 added to function.array-merge-recursive
Groups: php.notes 
Request: Send a blank email to php-notes+get-44179@lists.php.net to get a copy of this message
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; } -- http://www.php.net/manual/en/function.array-merge-recursive.php http://master.php.net/manage/user-notes.php?action=edit+29607 http://master.php.net/manage/user-notes.php?action=delete+29607 http://master.php.net/manage/user-notes.php?action=reject+29607

« previous php.notes (#44179) next »