php比较两个数组并丢弃无与伦比的数据

时间:2017-12-15 06:26:00

标签: php arrays

我需要帮助才能获得此数组的配对值: 数组$requestedFields

Array
(
    [0] => id
    [1] => first-name
    [2] => last-name
    [3] => username
    [4] => email
    [5] => truntt
    [6] => fiff
)

数组$apiFields

Array
(
    [0] => Array
        (
            [0] => id
            [1] => id
            [2] => accounts.profile.basic
        )

    [1] => Array
        (
            [0] => first-name
            [1] => fname
            [2] => accounts.profile.basic
        )

    [2] => Array
        (
            [0] => last-name
            [1] => lname
            [2] => accounts.profile.basic
        )

    [3] => Array
        (
            [0] => username
            [1] => username
            [2] => accounts.profile.basic
        )

    [4] => Array
        (
            [0] => email
            [1] => email
            [2] => accounts.profile.emailaddress
        )
    [5] => Array
        (
            [0] => test-test
            [1] => test
            [2] => accounts.profile.test
        )

)

我需要获得与$apiFields匹配的$requestedFields元素,在这种情况下只有0-4个元素值。 任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:4)

使用以下代码:

foreach($apiFields as $key=>$arr){
    if(!in_array($arr[0], $requestedFields)){
        unset($apiFields[$key]);
    }
}

它会在$apiFields数组中为您提供预期的结果。