如果在另一个数组中找到子数组键值,则取消设置数组

时间:2017-02-09 05:38:03

标签: php arrays

            $myarray = Array
            (
                [6] => Array
                    (
                        [id] => 84
                        [Name] => Confirm Mailing of SSDI App
                        [CaseId] => 0
                    )

                [10] => Array
                    (
                        [id] => 17
                        [Name] => Upload Fee Agreement (signed)
                        [CaseId] => 0
                    )

                [11] => Array
                    (
                        [id] => 41
                        [Name] => Upload 827 (1st) (signed)
                        [CaseId] => 0
                    )

                [12] => Array
                    (
                        [id] => 39
                        [Name] => Upload 3288 (signed)
                        [CaseId] => 0
                    )

                [13] => Array
                    (
                        [id] => 37
                        [Name] => Upload 16-F6 sig page (signed)
                        [CaseId] => 0
                    )

                [14] => Array
                    (
                        [id] => 38
                        [Name] => Upload 8001-BK sig page (signed)
                        [CaseId] => 0
                    )

            )

我有一个如上所示的主数组和另一个数组如下

            $anotherarray = Array
            (
                [1] => 41
                [2] => 39
                [3] => 37
            )

我必须取消ID与$another数组匹配的主数组,不用for循环,请帮我解决。

例如$anotherarray数组有一个三个id 41,39,37所以我必须从主数组中取消设置数组,如11,12,13。

3 个答案:

答案 0 :(得分:0)

使用新数组存储id和索引,这样就不必每次都遍历数组。

$keys_map = array_flip(array_map(function($v){return $v['id'];}, $array));
foreach($anotherarray as $v)
{
    unset($array[$keys_map[$v]]);
}

答案 1 :(得分:0)

尝试:

$myarray = array_filter($myarray, function($value) use ($anotherarray) {
    return !in_array($value['id'], $anotherarray);
});

答案 2 :(得分:0)

试试这个:

$arrFinal = array();
array_walk($myarray, function(&$value, $key)  use (&$anotherarray, &$arrFinal) {
    if(!in_array($value['id'],$anotherarray)){
        return $arrFinal[$key] = $value;
    }
});


print_r($arrFinal);