2 回答
TA贡献1829条经验 获得超7个赞
将 2 个数组转换为以 Clubid 作为键,这样可以轻松检查它们。然后做任何你想做的事情,将俱乐部数据的部分添加到新数组中。
$arr1 = array (
0 =>
array (
'Team' => 'Midtjylland',
'country' => 'dk',
'clubid' => '2',
'P' => '26',
'W' => '21',
'D' => '2',
'L' => '3',
'F' => '42',
'A' => '14',
'GD' => '28',
'Pts' => '65',
'form' => 'W,L,W,W,W,W,L,W,W,W,W,W,W,W,W,L,D,W,W,W,D,W,W,W,W,W',
),
1 =>
array (
'Team' => 'København',
'country' => 'dk',
'clubid' => '1',
'P' => '26',
'W' => '18',
'D' => '2',
'L' => '6',
'F' => '47',
'A' => '29',
'GD' => '18',
'Pts' => '56',
'form' => 'W,W,L,W,D,L,W,W,W,W,L,W,W,W,L,W,D,L,L,W,W,W,W,W,W,W',
),
2 =>
array (
'Team' => 'AGF',
'country' => 'dk',
'clubid' => '9',
'P' => '26',
'W' => '14',
'D' => '5',
'L' => '7',
'F' => '42',
'A' => '28',
'GD' => '14',
'Pts' => '47',
'form' => 'W,W,D,L,D,W,W,D,W,W,W,W,L,L,L,W,W,W,W,W,W,D,L,L,L,D',
),
3 =>
array (
'Team' => 'Brøndby',
'country' => 'dk',
'clubid' => '4',
'P' => '26',
'W' => '13',
'D' => '3',
'L' => '10',
'F' => '47',
'A' => '37',
'GD' => '10',
'Pts' => '42',
'form' => 'L,W,D,W,L,W,D,L,L,L,W,W,W,W,W,L,L,W,L,L,W,W,L,W,D,W',
),
4 =>
array (
'Team' => 'Nordsjælland',
'country' => 'dk',
'clubid' => '7',
'P' => '26',
'W' => '12',
'D' => '5',
'L' => '9',
'F' => '48',
'A' => '35',
'GD' => '13',
'Pts' => '41',
'form' => 'L,W,D,W,W,W,W,L,D,W,D,L,L,W,D,L,W,L,W,L,W,D,L,W,L,W',
),
5 =>
array (
'Team' => 'AaB',
'country' => 'dk',
'clubid' => '6',
'P' => '26',
'W' => '11',
'D' => '5',
'L' => '10',
'F' => '44',
'A' => '33',
'GD' => '11',
'Pts' => '38',
'form' => 'L,D,W,L,W,W,L,D,D,W,D,L,W,W,W,L,L,L,W,W,L,W,L,W,D,L',
),
6 =>
array (
'Team' => 'Randers',
'country' => 'dk',
'clubid' => '8',
'P' => '26',
'W' => '10',
'D' => '5',
'L' => '11',
'F' => '39',
'A' => '35',
'GD' => '4',
'Pts' => '35',
'form' => 'L,L,D,L,W,D,W,L,W,L,D,W,L,L,W,W,W,W,W,L,L,L,W,D,D,L',
),
7 =>
array (
'Team' => 'Horsens',
'country' => 'dk',
'clubid' => '10',
'P' => '26',
'W' => '10',
'D' => '4',
'L' => '12',
'F' => '25',
'A' => '44',
'GD' => '-19',
'Pts' => '34',
'form' => 'W,W,W,L,L,L,W,D,W,L,W,D,L,W,L,D,L,D,W,L,L,L,W,L,W,L',
),
8 =>
array (
'Team' => 'OB',
'country' => 'dk',
'clubid' => '3',
'P' => '26',
'W' => '9',
'D' => '6',
'L' => '11',
'F' => '34',
'A' => '30',
'GD' => '4',
'Pts' => '33',
'form' => 'W,L,D,D,D,L,L,D,L,L,L,W,W,L,W,W,L,W,D,D,L,W,W,L,W,L',
),
9 =>
array (
'Team' => 'Lyngby',
'country' => 'dk',
'clubid' => '34',
'P' => '26',
'W' => '9',
'D' => '5',
'L' => '12',
'F' => '31',
'A' => '45',
'GD' => '-14',
'Pts' => '32',
'form' => 'D,L,L,L,W,L,W,D,D,D,W,L,W,L,D,W,W,L,L,W,L,L,W,L,L,W',
),
10 =>
array (
'Team' => 'SønderjyskE',
'country' => 'dk',
'clubid' => '11',
'P' => '26',
'W' => '6',
'D' => '9',
'L' => '11',
'F' => '31',
'A' => '44',
'GD' => '-13',
'Pts' => '27',
'form' => 'D,L,D,L,L,W,L,W,L,D,L,L,L,L,W,W,D,D,D,L,D,D,L,W,D,W',
),
11 =>
array (
'Team' => 'Hobro',
'country' => 'dk',
'clubid' => '13',
'P' => '26',
'W' => '3',
'D' => '14',
'L' => '9',
'F' => '25',
'A' => '35',
'GD' => '-10',
'Pts' => '23',
'form' => 'D,W,D,D,L,L,D,D,D,L,D,L,D,L,L,L,D,W,L,D,D,L,W,D,D,D',
),
12 =>
array (
'Team' => 'Esbjerg',
'country' => 'dk',
'clubid' => '5',
'P' => '26',
'W' => '4',
'D' => '6',
'L' => '16',
'F' => '22',
'A' => '44',
'GD' => '-22',
'Pts' => '18',
'form' => 'L,D,D,L,L,W,L,L,L,W,L,D,L,L,L,D,W,L,L,D,W,L,L,L,D,L',
),
13 =>
array (
'Team' => 'Silkeborg',
'country' => 'dk',
'clubid' => '33',
'P' => '26',
'W' => '3',
'D' => '7',
'L' => '16',
'F' => '31',
'A' => '55',
'GD' => '-24',
'Pts' => '16',
'form' => 'D,L,W,D,D,L,L,W,L,L,L,L,D,W,L,L,L,L,L,D,D,D,L,L,L,L',
),
);
$arr2 = array (
0 =>
array (
'Team' => 'SønderjyskE',
'country' => 'dk',
'clubid' => '11',
'P' => '6',
'W' => '3',
'D' => '2',
'L' => '1',
'F' => '6',
'A' => '5',
'GD' => '1',
'Pts' => '11',
'form' => 'D,D,W,W,L,W',
),
1 =>
array (
'Team' => 'Silkeborg',
'country' => 'dk',
'clubid' => '33',
'P' => '6',
'W' => '3',
'D' => '1',
'L' => '2',
'F' => '12',
'A' => '4',
'GD' => '8',
'Pts' => '10',
'form' => 'W,W,L,L,D,W',
),
2 =>
array (
'Team' => 'OB',
'country' => 'dk',
'clubid' => '3',
'P' => '6',
'W' => '3',
'D' => '1',
'L' => '2',
'F' => '9',
'A' => '12',
'GD' => '-3',
'Pts' => '10',
'form' => 'L,D,W,W,W,L',
),
3 =>
array (
'Team' => 'Lyngby',
'country' => 'dk',
'clubid' => '34',
'P' => '6',
'W' => '0',
'D' => '2',
'L' => '4',
'F' => '3',
'A' => '9',
'GD' => '-6',
'Pts' => '2',
'form' => 'D,L,L,L,D,L',
),
);
//convert the arrays to have a usable key i.e. the clubid
foreach( $arr1 as $a){
$t1[$a['clubid']] = $a;
}
//make an array with a usable key i.e. the clubid
foreach( $arr2 as $a){
$t2[$a['clubid']] = $a;
}
foreach ($t2 as $k=>$t) {
if (isset($t1[$k])){
//$tmp = $t;
$tmp = $t;
$tmp['form'] = $t['form'] . ',' . $t2[$k]['form'];
$tmp['P'] = $t2[$k]['P'] + $t['P'];
$tmp['W'] = $t2[$k]['W'] + $t['W'];
$tmp['D'] = $t2[$k]['D'] + $t['D'];
$tmp['L'] = $t2[$k]['L'] + $t['L'];
$tmp['F'] = $t2[$k]['F'] + $t['F'];
$tmp['A'] = $t2[$k]['A'] + $t['A'];
$tmp['GD'] = $t2[$k]['GD'] + $t['GD'];
$tmp['Pts'] = $t2[$k]['Pts'] + $t['Pts'];
$new[] = $tmp;
} else {
$new[] = $t;
}
}
print_r($new);
TA贡献1818条经验 获得超7个赞
这是一种更干净/更简洁的方法。根据第二个数组声明输出数组并应用分组第一级键。
然后迭代第一个数组,并且仅当在输出数组中找到第一个数组的 Clubid 时才连接或添加值。
代码:(演示)
$result = [];
foreach ($arr2 as $array) {
$result[$array['clubid']] = $array;
}
foreach ($arr1 as $array) {
$clubId = $array['clubid'];
if (isset($result[$clubId])) {
$result[$clubId]['form'] .= ',' . $array['form']; // concat
foreach (['P', 'W', 'D', 'L', 'F', 'A', 'GD', 'Pts'] as $tally) {
$result[$clubId][$tally] += $array[$tally]; // add
}
}
}
var_export($result);
- 2 回答
- 0 关注
- 149 浏览
添加回答
举报