如何比较数组中的三个值并合并?

时间:2015-09-01 09:25:10

标签: php arrays

如何合并具有相同值的数组?

我有三个阵列,分别是$参与者,$ conferance_participants和$ contacts

我想比较这三个数组的值并合并

例如:

如果$参与者['calleridnum'] == $ conferance_participants ['uid'] == $ contacts ['name']

我希望输出如下:

Array
(
    [0] => Array
        (
            [calleridnum] => 1
            [test] => yay
            [uid] => 1
            [channel] => deze
            [name] => 1
            [limit] => 1
        )

)

到目前为止,这是我的代码:

<?php
$participants = [
    [   'calleridnum' => 1,
        'test' => 'yay' 
    ]
];
$conferance_participants = [
    [   'uid' => 1,
        'test' => 'yay2',
        'channel' => 'deze'
    ]
];
$contacts = [
    [   'name' => 1,
        'test' => 'yay2',
        'limit' => 1
    ]
];

foreach ($participants as $participant=>$p) {
    foreach ($conferance_participants as $conferance_participant=>$c) {
        foreach ($contacts as $contact=>$cs) {


        if (($p['calleridnum'] == $c['uid']) && ($c['uid'] == $cs['name'])) {


                 foreach ( $c as $key=>$val ) {
                     if (!isset($p[$key])) {
                    $participants[$participant][$key] = $val;
                    }
                } 

            } 
        }
    } 
}

print_r( $participants );
?>

1 个答案:

答案 0 :(得分:0)

尝试调用array_merge(),但您仍需要使用相同的键来考虑不同的值(例如,键'test'的值)

if (($p['calleridnum'] == $c['uid']) && ($p['uid'] == $c['name'])) {
    $participants[$participant] = array_merge(
        $participants[$participant],
        $conferance_participants[$conferance_participant],
        $contacts[$contact]
    );
}