Created
July 20, 2023 08:21
-
-
Save yehchge/caeb134908d9db38387383f1f45feeba to your computer and use it in GitHub Desktop.
array merge
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 | |
/** | |
* 將陣列合併, 如果有相同的值, 後面的值會覆蓋前面的值 | |
* @param array &$array1 [description] | |
* @param array &$array2 [description] | |
* @return [type] [description] | |
*/ | |
function array_merge_recursive_distinct (array &$array1, array &$array2){ | |
$merged = $array1; | |
foreach($array2 as $key => &$value){ | |
if (is_array ( $value ) && isset ( $merged [$key] ) && is_array ( $merged [$key] ) ){ | |
$merged [$key] = array_merge_recursive_distinct ( $merged [$key], $value ); | |
}else{ | |
$merged [$key] = $value; | |
} | |
} | |
return $merged; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment