php - 合并数组保留 arr1 中重复的值
php - merger array keeping values from arr1 where duplicate
我有 2 个数组 arr1
和 arr2
。我想合并它们而不复制或覆盖 arr1
.
中的数组值
arr1
(
[0] => Array (
[aName] => Doe jones
[ref] => 3
[afield] => 01-06-2015
)
[1] => Array (
[aName] => John doe
[ref] => 5
[afield] => 01-10-2015
)
[2] => Array (
[aName] => Peter Griffin
[ref] => 7
[afield]
)
)
arr2
(
[0] => Array (
[aName] => Doe jones
[ref] => null
[afield] => null
)
[1] => Array (
[aName] => John doe
[ref] => null
[afield] => null
)
[2] => Array (
[aName] => Peter Griffin
[ref] => null
[afield] => null
)
[3] => Array (
[aName] => Stuart Griffin
[ref] => null
[afield] => null
)
)
期望的输出:
newArr
(
[0] => Array (
[aName] => Doe jones
[ref] => 3
[afield] => 01-06-2015
)
[1] => Array (
[aName] => John doe
[ref] => 5
[afield] => 01-10-2015
)
[2] => Array (
[aName] => Peter Griffin
[ref] => 7
[afield]
)
[3] => Array (
[aName] => Stuart Griffin
[ref] => null
[afield] => null
)
)
我已经设法通过一系列嵌套的 foreach 循环和 if
语句实现了这一点。然而,这似乎是执行此任务的一种非常低效的方法。
我认为下面的功能组合可以工作,但没有达到预期的效果
$newArray = array_unique(array_merge($arr1, $arr2));
你不能使用那种方法来做你想做的事情,因为你有一些功能规则,比如合并具有相同名称的项目而不是空值删除空值。 array_merge 和 array_unique 不知道该规则。
你可以使用那种功能:
function mergeArray(array $arr1, array $arr2) {
$mergedArray = array();
foreach ($arr1 as $item1) {
$mergedArray[$item1['aName']] = $item1;
}
foreach ($arr2 as $item2) {
if (!isset($mergedArray[$item2['aName']]) {
$mergedArray[$item2['aName']] = $item2;
} else {
$item = $mergedArray[$item2['aName']];
foreach ($item as $key => $value) {
if (null === $value && isset($item2[$key])) {
$mergedArray[$item2['aName']][$key] = $item2[$key];
}
}
}
}
return $mergedArray;
}
我有 2 个数组 arr1
和 arr2
。我想合并它们而不复制或覆盖 arr1
.
arr1
(
[0] => Array (
[aName] => Doe jones
[ref] => 3
[afield] => 01-06-2015
)
[1] => Array (
[aName] => John doe
[ref] => 5
[afield] => 01-10-2015
)
[2] => Array (
[aName] => Peter Griffin
[ref] => 7
[afield]
)
)
arr2
(
[0] => Array (
[aName] => Doe jones
[ref] => null
[afield] => null
)
[1] => Array (
[aName] => John doe
[ref] => null
[afield] => null
)
[2] => Array (
[aName] => Peter Griffin
[ref] => null
[afield] => null
)
[3] => Array (
[aName] => Stuart Griffin
[ref] => null
[afield] => null
)
)
期望的输出:
newArr
(
[0] => Array (
[aName] => Doe jones
[ref] => 3
[afield] => 01-06-2015
)
[1] => Array (
[aName] => John doe
[ref] => 5
[afield] => 01-10-2015
)
[2] => Array (
[aName] => Peter Griffin
[ref] => 7
[afield]
)
[3] => Array (
[aName] => Stuart Griffin
[ref] => null
[afield] => null
)
)
我已经设法通过一系列嵌套的 foreach 循环和 if
语句实现了这一点。然而,这似乎是执行此任务的一种非常低效的方法。
我认为下面的功能组合可以工作,但没有达到预期的效果
$newArray = array_unique(array_merge($arr1, $arr2));
你不能使用那种方法来做你想做的事情,因为你有一些功能规则,比如合并具有相同名称的项目而不是空值删除空值。 array_merge 和 array_unique 不知道该规则。
你可以使用那种功能:
function mergeArray(array $arr1, array $arr2) {
$mergedArray = array();
foreach ($arr1 as $item1) {
$mergedArray[$item1['aName']] = $item1;
}
foreach ($arr2 as $item2) {
if (!isset($mergedArray[$item2['aName']]) {
$mergedArray[$item2['aName']] = $item2;
} else {
$item = $mergedArray[$item2['aName']];
foreach ($item as $key => $value) {
if (null === $value && isset($item2[$key])) {
$mergedArray[$item2['aName']][$key] = $item2[$key];
}
}
}
}
return $mergedArray;
}