/**
* Overwrites an array with values from input arrays.
* Keys that do not exist in the first array will not be added!
*
* $a1 = array('name' => 'john', 'mood' => 'happy', 'food' => 'bacon');
* $a2 = array('name' => 'jack', 'food' => 'tacos', 'drink' => 'beer');
*
* // Overwrite the values of $a1 with $a2
* $array = Zwp_Util_Tool::overwrite($a1, $a2);
*
* // The output of $array will now be:
* array('name' => 'jack', 'mood' => 'happy', 'food' => 'tacos')
*
* @param array master array
* @param array input arrays that will overwrite existing values
* @return array
*/
public static function overwrite($array1, $array2)
{
foreach (array_intersect_key($array2, $array1) as $key => $value)
{
$array1[$key] = $value;
}
if (func_num_args() > 2)
{
foreach (array_slice(func_get_args(), 2) as $array2)
{
foreach (array_intersect_key($array2, $array1) as $key => $value)
{
$array1[$key] = $value;
}
}
}
return $array1;
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章