合并基于两个唯一值的PHP数组 [英] Merge php array based on two unique values

查看:144
本文介绍了合并基于两个唯一值的PHP数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一种方式做了pretty奇数组多维数组之间的合并。看看下面的两个数组 arrayOne arrayTwo 为例。

我想数组合并到 arrayThree ,它会显示阵列项目所特有,如果两个 组合都是唯一的。它会与另一个合并从一个数组中的值,如果该值不是present,那么它会提供一个空字符串。 (见 arrayThree 对我是什么意思)

任何想法?

  $ arrayOne =阵列(
        阵列('数'=大于1,'信'=>'一','qcol'=>'网球'),
        阵列('编号'=大于1,字母= GT;'B','qcol'=>'足球'),
        阵列('数'=大于2,'信'=>'一','qcol'=>'篮球'),
        阵列('数'=大于2,'信'=>'B','qcol'=>'足球'),
        阵列('编号'=→3'字母'= GT;'一','qcol'=>'保龄球'),
        阵列('编号'=→3'字母'= GT;'B','qcol'=>'橄榄球')
    );$ arrayTwo =阵列(
        阵列('编号'=大于1,字母= GT;'一','RVAL'=>总线),
        阵列('数'=大于1,'信'=>'B','RVAL'=>'车'),
        阵列('编号'=大于2,'字母'= GT;'一','RVAL'=>'卡车'),
        阵列('编号'=大于2,'字母'= GT;'B','RVAL'=>'平面'),
        阵列('数'=> 4,'信'=>'B','RVAL'=>'船')
    );

将合并到:

  $ arrayThree =阵列(
        阵列('数'=大于1,'信'=>'一','RVAL'=>'巴士','qcol'=>'网球'),
        阵列('数'=大于1,'信'=>'B','RVAL'=>'车','qcol'=>'足球'),
        阵列('数'=大于2,'信'=>'一','RVAL'=>'卡车','qcol'=>'篮球'),
        阵列('数'=大于2,'信'=>'B','RVAL'=>'飞机','qcol'=>'足球'),
        阵列('编号'=→3'字母'= GT;'一','RVAL'=>'','qcol'=>'保龄球'),
        阵列('编号'=→3'字母'= GT;'B','RVAL'=>'','qcol'=>'橄榄球'),
        阵列('编号'=→4,'字母'= GT;'B','RVAL'=>'船','qcol'=>'')
    );


解决方案

  $ arrayThree =阵列();的foreach($ arrayOne为$ I){
    $ arrayThree [$ I ['号']。 $ I ['信'] = $ I +阵列('RVAL'=> NULL);
}
的foreach($ arrayTwo为$ I){
    $键= $ I ['号']。 $ I ['信'];
    如果(使用isset($ arrayThree [$关键])){
        $ arrayThree [$关键] ['RVAL'] = $ I ['RVAL'];
    }其他{
        $ arrayThree [$关键] = $ I +阵列('qcol'=> NULL);
    }
}$ arrayThree = array_values​​($ arrayThree);

I'm looking for a way to do a pretty odd array merge between multidimensional arrays. Take the following two arrays arrayOne and arrayTwo as examples.

I'd like to merge the arrays into arrayThree, which will show arrays items that are unique if both number and letter combined are unique. It'll merge the values from one array with another and if the value isn't present, then it'll provide an empty string. (see arrayThree for what I mean)

Any ideas?

$arrayOne = array(
        array('number' => 1, 'letter' => 'a', 'qcol' => 'tennis'),
        array('number' => 1, 'letter' => 'b', 'qcol' => 'soccer'),
        array('number' => 2, 'letter' => 'a', 'qcol' => 'basketball'),
        array('number' => 2, 'letter' => 'b', 'qcol' => 'football'),
        array('number' => 3, 'letter' => 'a', 'qcol' => 'bowling'),
        array('number' => 3, 'letter' => 'b', 'qcol' => 'rugby')
    );

$arrayTwo = array(
        array('number' => 1, 'letter' => 'a', 'rval' => 'bus'),
        array('number' => 1, 'letter' => 'b', 'rval' => 'car'),
        array('number' => 2, 'letter' => 'a', 'rval' => 'truck'),
        array('number' => 2, 'letter' => 'b', 'rval' => 'plane'),
        array('number' => 4, 'letter' => 'b', 'rval' => 'boat')
    );

would merge into:

$arrayThree = array(
        array('number' => 1, 'letter' => 'a', 'rval' => 'bus', 'qcol' => 'tennis'),
        array('number' => 1, 'letter' => 'b', 'rval' => 'car', 'qcol' => 'soccer'),
        array('number' => 2, 'letter' => 'a', 'rval' => 'truck', 'qcol' => 'basketball'),
        array('number' => 2, 'letter' => 'b', 'rval' => 'plane', 'qcol' => 'football'),
        array('number' => 3, 'letter' => 'a', 'rval' => '', 'qcol' => 'bowling'),
        array('number' => 3, 'letter' => 'b', 'rval' => '', 'qcol' => 'rugby'),
        array('number' => 4, 'letter' => 'b', 'rval' => 'boat', 'qcol' => '')
    );

解决方案

$arrayThree = array();

foreach ($arrayOne as $i) {
    $arrayThree[$i['number'] . $i['letter']] = $i + array('rval' => null);
}
foreach ($arrayTwo as $i) {
    $key = $i['number'] . $i['letter'];
    if (isset($arrayThree[$key])) {
        $arrayThree[$key]['rval'] = $i['rval'];
    } else {
        $arrayThree[$key] = $i + array('qcol' => null);
    }
}

$arrayThree = array_values($arrayThree);

这篇关于合并基于两个唯一值的PHP数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆