比较两个数组并删除不需要的项目

时间:2013-09-16 21:10:30

标签: php arrays foreach

我非常接近这个我可以尖叫。

这就是我追求的目标。我有两个数组。第一个是以下内容:

array("id", "txtLname", "txtFname");

第二个如下:

Array
(
    [0] => Array
        (
            [id] => 220
            [RecordGUID] => 1233C9-1F7A15-E8A447-C56CB2-227C20-2829E0
            [txtEmplid] => 5469857
            [txtLname] => Jones
            [txtFname] => Richard
            [txtMname] => 
            [txtEmail] => email address
            [Reg_Pass] => umbra1234
            [Reg_User] => user
            [txtSecEmail] => user@gmail.com
            [dtDOB] => 1979-02-28
            [drpStatus] => STUDENT
            [lstWaive] => 1
            [ENTERED] => 2013-09-15 18:03:18
            [Status] => 0
            [Approvalcode] => 17dc8e7336f0e9fd3411a4d9617efe865c5744ac
            [Approvaldate] => 2013-09-15 18:03:47
            [Approved] => 1
        )

    [1] => Array
        (
            [id] => 221
            [RecordGUID] => DD1E72-368879-68CFE2-E03010-ECE1B1-0974E9
            [txtEmplid] => 4454688
            [txtLname] => Mathews
            [txtFname] => Richard
            [txtMname] => 
            [txtEmail] => user2@gmail.com
            [Reg_Pass] => umbra1234
            [Reg_User] => user
            [txtSecEmail] => user3@gmail.com
            [dtDOB] => 1979-02-28
            [drpStatus] => STUDENT
            [lstWaive] => 1
            [ENTERED] => 2013-09-16 12:28:08
            [Status] => 0
            [Approvalcode] => 7182769e45dc38a3a747c4bdb128e2f0a8c658e4
            [Approvaldate] => 
            [Approved] => 0
        )

    [2] => Array
        (
            [id] => 222
            [RecordGUID] => 40D8E7-04C600-30A829-8E26CC-498BBE-9D3DF6
            [txtEmplid] => 
            [txtLname] => 
            [txtFname] => 
            [txtMname] => 
            [txtEmail] => 
            [Reg_Pass] => 
            [Reg_User] => 
            [txtSecEmail] => 
            [dtDOB] => 1979-02-28
            [drpStatus] => STUDENT
            [lstWaive] => 1
            [ENTERED] => 2013-09-16 12:28:24
            [Status] => 0
            [Approvalcode] => 8d2c33f7b7d6ef620811dbc4358e8103346bfb59
            [Approvaldate] => 
            [Approved] => 0
        )

)

我需要的是遍历第二个数组并删除与第一个数组不匹配的项目。结果如下:

Array
(
    [0] => Array
        (
            [id] => 220
            [txtLname] => Method
            [txtFname] => Richard

        )

    [1] => Array
        (
            [id] => 221
            [txtLname] => Method
            [txtFname] => Richard

        )

    [2] => Array
        (
            [id] => 222
            [txtLname] => Method
            [txtFname] => Richard

        )

)

这是我到目前为止所做的。

foreach ($r as $reg) {
foreach($reg as $k => $v) {
    if ($k == !in_array($k, $f)) {
        echo $reg[$k];
    }
}

}

我得到的回应响应是来自第一个数组的所有数据的列表,减去第一个数组中的字段。所以,它正在删除那些项目 我想要删除,但我似乎无法将其转换为正确的格式。

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

echo $reg[$k];替换为unset($reg[$k]);

答案 1 :(得分:1)

$arr1 = array("id"=>1, "txtLname"=>1, "txtFname"=>1)
$result = array();

// $arr2 = array 2 (check your code)


foreach ($arr2 as $el) {
   $result[] = array_intersect_key($el, $arr1);
}

var_dump($result);