note 55983 deleted from function.array-merge-recursive by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 14:53:47 +0000 |
| Subject: | note 55983 deleted from function.array-merge-recursive by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-197207@lists.php.net to get a copy of this message | ||
Note Submitter: jason at ebasterpro dot com
----
This modifications allows you to merge arrays of objects and objects of arrays recursively.
/**
* arrayobj_merge_recursive2()
*
* Similar to array_merge_recursive2 but supports objects and arrays, keyed-valued are always
overwritten.
* Priority goes to the 2nd array. And support Object Array mixture
*
* @static yes
* @public yes
* @param $paArray1 array/object
* @param $paArray2 array/object
* @return array/object
*/
function arrayobj_merge_recursive2($paArray1, $paArray2)
{
if(is_array($paArray2))
{
foreach ($paArray2 AS $sKey2 => $sValue2)
{
$paArray1[$sKey2] = arrayobj_merge_recursive2(@$paArray1[$sKey2], $sValue2);
}
}
elseif(is_object($paArray2))
{
foreach ($paArray2 AS $sKey2 => $sValue2)
{
$paArray1->{$sKey2} = arrayobj_merge_recursive2(@$paArray1->{$sKey2}, $sValue2);
}
} else {
return $paArray2;
}
return $paArray1;
}