dqkx69935 2012-04-30 21:16 采纳率: 100%
浏览 15
已采纳

在PHP中组合数组

Lets say I want to combine 2 arrays and the arrays are names $year_into and $likes_dislikes. They share a key called "name". How can I make it so that this one:

$year_info

Array
(
    [0] => Array
        (
            [name] => JOE MONROE
            [year] => 1950
            [info] => his ghost still haunts us
        )
    [1] => Array
        (
            [name] => FUTUREMAN
            [year] => 1930
            [info] => RIP
        )
)

and this one $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
        )
)

Can be combined into one array $complete that looks like this, where the information from the 2nd array is added to the 1st if the "name" value matches:

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
        )
)

I've looked through the already asked questions but don't see anything, maybe I'm using the wrong terminology to describe the problem. I'm stuck on the foreach loop because if I say like this

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'] )
}

I'll just get the same values for likes/dislikes for all. What is the simplest way to do this?

  • 写回答

4条回答 默认 最新

  • dongliu8559 2012-04-30 21:24
    关注

    What I would try to do is loop through both sets of arrays (a nested foreach loop would be a good choice), checking for instances where The name attribute is the same in both arrays. When they are, you can use array_merge() to merge them into a new array. An example would be:

    $newArray = array();
    foreach ($arr1 as $first) {
        foreach ($arr2 as $second) {
            if($first["name"] == $second["name"]){
                array_push($newArray, array_merge($first, $second));
            }
        }
    }
    

    Assuming you named your arrays $arr1 and $arr2.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?