为了账号安全,请及时绑定邮箱和手机立即绑定

PHP数组按要求合并

PHP数组按要求合并

PHP
不负相思意 2019-03-09 18:32:25
数组一: [0] => Array ( [rid] => 1 [hf_rid] => 0 [aid] => 1 [uid] => 1 [content] => 我肯定第一名 [re_time] => 2017-10-10 10:59 [nickname] => 就是这么帅 ) [1] => Array ( [rid] => 2 [hf_rid] => 0 [aid] => 1 [uid] => 1 [content] => 马蛋,中枪了 [re_time] => 2017-10-10 01:10 [nickname] => 说定的唉 ) 数组二: [0] => Array ( [rid] => 3 [hf_rid] => 1 [aid] => 1 [uid] => 1 [content] => 我肯定第一名 [re_time] => 2017-10-10 10:59 [nickname] => 就是这么帅 ) [1] => Array ( [rid] => 4 [hf_rid] => 2 [aid] => 1 [uid] => 1 [content] => 马蛋,中枪了 [re_time] => 2017-10-10 01:10 [nickname] => 说定的唉 ) [2] => Array ( [rid] => 5 [hf_rid] => 2 [aid] => 1 [uid] => 1 [content] => ssd发的发的服务 [re_time] => 2017-10-10 01:10 [nickname] => 说定的唉 ) 我想把他们合并起来!~具体合并要求是这样的: 如果数组二的hf_rid字段等于数组一的 rid 字段,那么合并的效果是这样的 [0] => Array ( [rid] => 1 [hf_rid] => 0 [aid] => 1 [uid] => 1 [content] => 我肯定第一名 [re_time] => 2017-10-10 10:59 [nickname] => 就是这么帅 [xxxxx]=> Array ( [rid] => 3 [hf_rid] => 1 [aid] => 1 [uid] => 1 [content] => 我肯定第一名 [re_time] => 2017-10-10 10:59 [nickname] => 就是这么帅 ) ) [1] => Array ( [rid] => 2 [hf_rid] => 0 [aid] => 1 [uid] => 1 [content] => 马蛋,中枪了 [re_time] => 2017-10-10 01:10 [nickname] => 说定的唉 [xxxxx]=> Array ( [0]=>array ( [rid] => 4 [hf_rid] => 2 [aid] => 1 [uid] => 1 [content] => 马蛋,中枪了 [re_time] => 2017-10-10 01:10 [nickname] => 说定的唉 ) [1] => Array ( [rid] => 5 [hf_rid] => 2 [aid] => 1 [uid] => 1 [content] => ssd发的发的服务 [re_time] => 2017-10-10 01:10 [nickname] => 说定的唉 ) ) ) 求大神们伸出援助之手啊!!!阿门!!
查看完整描述

4 回答

?
潇湘沐

TA贡献1816条经验 获得超6个赞

foreach($aArr1 as &$aV1)
{
    foreach($aArr2 as $aV2)
    {
        if($aV2['hf_rid'] == $aV1['rid'])
        {
            $aV1['new'][] = $aV2;
        }
    }
}
结果
Array
(
    [0] => Array
        (
            [rid] => 1
            [hf_rid] => 0
            [aid] => 1
            [uid] => 1
            [content] => 我肯定第一名
            [re_time] => 2017-10-10 10:59
            [nickname] => 就是这么帅
            [new] => Array
                (
                    [0] => Array
                        (
                            [rid] => 3
                            [hf_rid] => 1
                            [aid] => 1
                            [uid] => 1
                            [content] => 我肯定第一名
                            [re_time] => 2017-10-10 10:59
                            [nickname] => 就是这么帅
                        )

                )

        )

    [1] => Array
        (
            [rid] => 2
            [hf_rid] => 0
            [aid] => 1
            [uid] => 1
            [content] => 马蛋,中枪了
            [re_time] => 2017-10-10 01:10
            [nickname] => 说定的唉
            [new] => Array
                (
                    [0] => Array
                        (
                            [rid] => 4
                            [hf_rid] => 2
                            [aid] => 1
                            [uid] => 1
                            [content] => 马蛋,中枪了
                            [re_time] => 2017-10-10 01:10
                            [nickname] => 说定的唉
                        )

                    [1] => Array
                        (
                            [rid] => 5
                            [hf_rid] => 2
                            [aid] => 1
                            [uid] => 1
                            [content] => ssd发的发的服务
                            [re_time] => 2017-10-10 01:10
                            [nickname] => 说定的唉
                        )

                )

        )

)
查看完整回答
反对 回复 2019-03-18
?
jeck猫

TA贡献1909条经验 获得超7个赞

foreach($arr_1 as $key=>$val){

foreach($arr_2 as $v){
    if($v['hf_rid'] == $val['rid']){
        $arr_1['xxx'] = $v;
        }
}

}

查看完整回答
反对 回复 2019-03-18
?
FFIVE

TA贡献1797条经验 获得超6个赞

不好意思,(新的new数组应该是有序数组,它会有N多hf_rid=rid的)

查看完整回答
反对 回复 2019-03-18
?
大话西游666

TA贡献1817条经验 获得超14个赞

    $arr1 = [
        [
            'rid' => 1,
            'hf_rid' => 0,
            'aid' => 1,
            'uid' => 1,
            'content' => '我肯定第一名',
            're_time' => '2017-10-10 10:59',
            'nickname' => '就是这么帅',
        ],
        [
            'rid' => 2,
            'hf_rid' => 0,
            'aid' => 1,
            'uid' => 1,
            'content' => '马蛋,中枪了',
            're_time' => '2017-10-10 01:10',
            'nickname' => '说定的唉',
        ],
    ];

    $arr2 = [
        [
            'rid' => 3,
            'hf_rid' => 1,
            'aid' => 1,
            'uid' => 1,
            'content' => '我肯定第一名',
            're_time' => '2017-10-10 10:59',
            'nickname' => '就是这么帅',
        ],
        [
            'rid' => 4,
            'hf_rid' => 2,
            'aid' => 1,
            'uid' => 1,
            'content' => '马蛋,中枪了',
            're_time' => '2017-10-10 01:10',
            'nickname' => '说定的唉',
        ],
        [
            'rid' => 5,
            'hf_rid' => 2,
            'aid' => 1,
            'uid' => 1,
            'content' => 'ssd发的发的服务',
            're_time' => '2017-10-10 01:10',
            'nickname' => '说定的唉',
        ],
    ];
    $new_arr = [];
    # 如果数组二的hf_rid字段等于数组一的 rid
    foreach ($arr1 as $item => $value) {
        foreach ($arr2 as $n => $m) {
            if ($arr1[$item]['rid'] == $arr2[$n]['hf_rid']) {
                $arr1[$item]['xxxx'][] = $m;
                $new_arr = $arr1;
            }
        }
    }
查看完整回答
反对 回复 2019-03-18
  • 4 回答
  • 0 关注
  • 381 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信