Created
August 15, 2012 09:00
-
-
Save ichiriac/3357809 to your computer and use it in GitHub Desktop.
Clean way to recursivelly merge an array (secure & fast way)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* An array merging helper | |
* @params array $original | |
* @params array $additionnal | |
* @return array | |
*/ | |
function merge_array( $original, $additionnal ) | |
{ | |
if ( empty($additionnal) ) return $original; | |
foreach($additionnal as $key => $value) { | |
if ( is_numeric( $key ) ) { | |
$original[] = $value; | |
} else { | |
$original[$key] = ( | |
!empty($original[$key]) | |
&& is_array($value) | |
&& is_array($original[$key]) ? | |
merge_array($original[$key], $additionnal[$key]) : $value | |
); | |
} | |
} | |
return $original; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment