我正在尝试合并两个有重叠结果的数组和一些不同的数组,如下所示:
array(
[0] => array('name' => 'John', 'score' => '9');
[1] => array('name' => 'Pete', 'score' => '2');
[2] => array('name' => 'Eric', 'score' => '7');
)
和
array(
[0] => array('name' => 'Lisa', 'score' => '1');
[1] => array('name' => 'Pete', 'score' => '5');
[2] => array('name' => 'Mary', 'score' => '4');
)
这应该导致一个包含五个(不是六个)结果的数组。皮特的得分应该是他两个得分的总和,即'7'。
是否有一个简单的功能,或者我是否必须预先处理一个(或两个?)列表并相互检查它们?我不知道如何开始这个,正确方向的指针将不胜感激!
所以..实际上两个数组都填充了对象..任何新的想法?
答案 0 :(得分:2)
创建一个临时查找数组并从中重新创建最终数组。这是一个使用额外空间的O(n)算法。请在此处查看:http://codepad.org/4aL5KMR4
<?php
$arr1 =
array(
array('name' => 'John', 'score' => '9'),
array('name' => 'Pete', 'score' => '2'),
array('name' => 'Eric', 'score' => '7'),
);
$arr2=
array(
array('name' => 'Lisa', 'score' => '1'),
array('name' => 'Pete', 'score' => '5'),
array('name' => 'Mary', 'score' => '4'),
);
$res = array();
$revLookUp = array();
foreach($arr2 as $row)
{
if(isset($revLookUp[$row['name']]) == true)
$revLookUp[$row['name']] += $row['score'];
else
$revLookUp[$row['name']] = $row['score'];
}
foreach($arr1 as $row)
{
if(isset($revLookUp[$row['name']]) == true)
$revLookUp[$row['name']] += $row['score'];
else
$revLookUp[$row['name']] = $row['score'];
}
foreach($revLookUp as $key => $value){
$res[] = array('name' => $key, 'score' => $value);
}
print_r($res);
?>
答案 1 :(得分:1)
这将完成工作
$array1 = array(
array('name' => 'John', 'score' => 9),
array('name' => 'Pete', 'score' => 2),
array('name' => 'Eric', 'score' => 7),
);
$array2 = array(
array('name' => 'Lisa', 'score' => 1),
array('name' => 'Pete', 'score' => 5),
array('name' => 'Mary', 'score' => 4),
);
$result = array();
$names = array();
foreach ($array2 as $elem2){
$cc = 0;
foreach ($array1 as $key => $elem1){
$cc++;
if ($elem1['name'] == $elem2['name']) {
$names[$elem1['name']] = $elem1['score'] + $elem2['score'];
break;
}
else if ($cc == count($array1)) {
array_push ($result, $elem2);
}
}
}
foreach ($array1 as $elem1){
if ($names[$elem1['name']]){
$elem1['score'] = $names[$elem1['name']];
}
array_push ($result, $elem1);
}
print_r($result);
答案 2 :(得分:1)
由于不同,此任务没有任何功能。使用此算法:
function merge($array1, $array2)
{
$result = array();
$count = (count($array1) > count($array2)) ? count($array1) : count($array2);
for($i=0; $i<$count; $i++)
{
if($array1[$i]['name'] != $array2[$i]['name'])
{
$result[] = $array1[$i];
$result[] = $array2[$i];
}
else
{
$array1['score'] += $array2['score'];
$result[] = $array1[$i];
}
}
return $result;
}
抱歉我的英语不好。 祝你好运!
答案 3 :(得分:1)
function vector_sum($a, $b) {
$arrays=func_get_args();
$res=array(); $result=array();
foreach($arrays as $current) {
$res=array_reduce($current, function($res, cur){
@$res[$cur['name']]+=$cur['score'];
return $res;
}, $res);
}
// sums done. put the result array in shape
foreach ($res ad $name=>$score)
array_push($result, array('name'=>$name, 'score'=>$score));
}
return $result;
}
未经测试,它应该使用任意数组编号
答案 4 :(得分:0)
如果您按照以下方式稍微重新设计数组:
希望有所帮助
<?php
$a = array(
'John' => '9',
'Pete' => '2',
'Eric' => '7',
);
$b = array(
'Lisa' => '1',
'Pete' => '5',
'Mary' => '4',
);
$newArray = $a;
foreach($b as $key => $value) {
if(array_key_exists($key, $newArray)){
$newArray[$key] += $value;
}else{
$newArray[$key] = $value;
}
}
print_r($newArray);
?>
您也可以在此处查看结果:http://codepad.org/9hKF8oVC