假设我想组合 2 个数组,这些数组是名称 $year_into 和 $likes_dislikes。他们共享一个名为“名称”的密钥。我怎样才能做到这一点:
$year_info
Array
(
[0] => Array
(
[name] => JOE MONROE
[year] => 1950
[info] => his ghost still haunts us
)
[1] => Array
(
[name] => FUTUREMAN
[year] => 1930
[info] => RIP
)
)
还有这个 $likes_dislikes
Array
(
[0] => Array
(
[name] => JOE MONROE
[likes] => cornbread
[dislikes] => pain
)
[1] => Array
(
[name] => E. Case
[likes] => chaos
[dislikes] => order
)
[2] => Array
(
[name] => FUTUREMAN
[likes] => mustard
[dislikes] => mayo
)
)
可以组合成一个数组 $complete,如下所示,如果“name”值匹配,则将来自第二个数组的信息添加到第一个数组中:
Array
(
[0] => Array
(
[name] => JOE MONROE
[year] => 1950
[info] => his ghost still haunts us
[likes] => cornbread
[dislikes] => pain
)
[1] => Array
(
[name] => FUTUREMAN
[year] => 1930
[info] => RIP
[likes] => mustard
[dislikes] => mayo
)
)
我查看了已经提出的问题,但没有看到任何内容,也许我使用了错误的术语来描述问题。我被困在 foreach 循环中,因为如果我这样说
foreach ($year_info as $y){
$complete[]=array('name'=>$y['name'], 'year'=>$y['year'], 'info'=>$y['info'], 'likes'=$likes_dislikes[0]['likes'],'dislikes'=>$likes_dislikes[0]['dislikes'] )
}
我会为所有人的喜欢/不喜欢得到相同的值。最简单的方法是什么?