2 回答
TA贡献1804条经验 获得超2个赞
如果我正确理解您的要求,那么 您可以在 的帮助下完成foreach(),array_search()如下array_column()所示,
foreach($second_array as $k=>$v){
$id_person = $v['id_person'];
$key = array_search($id_person, array_column($first_array, 'id_person'));
if($key){
$second_array[$k]['starter'] = $first_array[$key]['starter'];
$second_array[$k]['subs'] = $first_array[$key]['subs'];
$second_array[$k]['yellow_card'] = $first_array[$key]['yellow_card'];
$second_array[$k]['red_card'] = $first_array[$key]['red_card'];
}
}
print_r($second_array);
或者使用array_merge()而不是 4 行。
foreach($second_array as $k=>$v){
$id_person = $v['id_person'];
$key = array_search($id_person, array_column($first_array, 'id_person'));
if($key){
array_merge($second_array[$k],['starter'=>$first_array[$key]['starter'],'subs'=>$first_array[$key]['subs'],'yellow_card'=>$first_array[$key]['yellow_card'],'red_card'=>$first_array[$key]['red_card']]);
}
}
print_r($second_array);
工作演示: https: //3v4l.org/FeanU
TA贡献1810条经验 获得超4个赞
这是解决问题的另一种方法:
/*
Go through both arrays and put the person_id as key in each array
(e.g. [0]['person_id'] = 197 TO [197]['person_id'] etc)
*/
foreach($first_array as $key=>$item) {
$first_array[$item['id_person']] = $item;
unset($first_array[$key]); //remove current item
}
foreach($second_array as $key=>$item) {
$second_array[$item['id_person']] = $item;
unset($second_array[$key]); //remove current item
}
/*
Go through second array. If first array contains the same person_id
the key for person_id exists, that's why we can use isset() here
*/
foreach($second_array as $key=>$item) {
if (isset($first_array[$key])) {
//Just copy the part that you intented from first array
//to this $second_array
$second_array[$key]['starter'] = $first_array[$key]['starter'];
$second_array[$key]['subs'] = $first_array[$key]['subs'];
$second_array[$key]['goal'] = $first_array[$key]['goal'];
$second_array[$key]['yellow_card'] = $first_array[$key]['yellow_card'];
$second_array[$key]['red_card'] = $first_array[$key]['red_card'];
}
}
这将导致:
Array
(
[195] => Array
(
[id_person] => 195
[position] => 1
[first] => Mikkel
[last] => Andersen
[id_club] => 2
[type] => 2
[captain] => 0
[pos] => 1
[subsmin_in] =>
[subsmin_out] =>
[starter] => 1
[subs] => 0
[goal] => 3
[yellow_card] => 2
[red_card] =>
)
[236] => Array
(
[id_person] => 236
[position] => 14
[first] => Sebastian
[last] => Buch
[id_club] => 2
[type] => 2
[captain] => 0
[pos] => 14
[subsmin_in] =>
[subsmin_out] =>
[starter] => 0
[subs] => 0
[goal] => 0
[yellow_card] =>
[red_card] =>
)
[209] => Array
(
[id_person] => 209
[position] => 3
[first] => Joel
[last] => Andersson
[id_club] => 2
[type] => 2
[captain] => 0
[pos] => 18
[subsmin_in] =>
[subsmin_out] =>
[starter] => 1
[subs] => 0
[goal] => 0
[yellow_card] =>
[red_card] =>
)
)
如果您希望索引从零开始,您可以这样做:
$second_array = array_values($second_array);
你将拥有:
Array
(
[0] => Array
(
[id_person] => 195
[position] => 1
[first] => Mikkel
[last] => Andersen
[id_club] => 2
[type] => 2
[captain] => 0
[pos] => 1
[subsmin_in] =>
[subsmin_out] =>
[starter] => 1
[subs] => 0
[goal] => 3
[yellow_card] => 2
[red_card] =>
)
[1] => Array
(
[id_person] => 236
[position] => 14
[first] => Sebastian
[last] => Buch
[id_club] => 2
[type] => 2
[captain] => 0
[pos] => 14
[subsmin_in] =>
[subsmin_out] =>
[starter] => 0
[subs] => 0
[goal] => 0
[yellow_card] =>
[red_card] =>
)
[2] => Array
(
[id_person] => 209
[position] => 3
[first] => Joel
[last] => Andersson
[id_club] => 2
[type] => 2
[captain] => 0
[pos] => 18
[subsmin_in] =>
[subsmin_out] =>
[starter] => 1
[subs] => 0
[goal] => 0
[yellow_card] =>
[red_card] =>
)
)
笔记!为了测试目的,我更改了一些值。
- 2 回答
- 0 关注
- 79 浏览
添加回答
举报