如何通过检查其值来从数组中删除对象?

时间:2015-08-26 05:40:56

标签: php arrays

如何从第二个数组中删除对象$ conferance_participants

如果此陈述为真

如果$participants['calleridnum'] == $conferance_participants['uid']

<?php
$participants = [
    [   'calleridnum' => 1,
        'test' => 'yay' 
    ]
];
$conferance_participants = [
    [   'uid' => 1,
        'test' => 'yay2',
        'dit' => 'deze'
    ],
    [  'uid' => 2,
        'test' => 'test',
        'dit' => 'wew'
    ]
];

$uniques = array_unique(array_merge($participants,   $conferance_participants));

print_r( $uniques );
?>

如果此陈述为真

如果$participants['calleridnum'] == $conferance_participants['uid']

我想从第二个具有相同值

的数组中删除该对象

我希望输出像

Array
(
    [0] => Array
        (
            [calleridnum] => 1
            [test] => yay
        )


    [2] => Array
        (
            [uid] => 2
            [test] => test
            [dit] => wew
        )

)

1 个答案:

答案 0 :(得分:1)

foreach ($participants as $key=>$p) {
    foreach ($conferance_participants as $key=>$cp) {

        if ($p['calleridnum'] == $cp['uid']) {

          unset($participants[$key]);
        } 

    } 
}
$result = array_merge($conferance_participants, $participants);

或者,如果您不想更改$ conferance_participants数组,请执行

$result = array();
foreach ($conferance_participants as $key => $cp) {
    if ($cp['uid'] != $participants['calleridnum'])
        $result[] = $cp;
}
$result[] = $participants;