简体   繁体   English

如何从另一个保存值的数组中删除键?

[英]How to remove key from another array that holds values?

let's suppos we have the following two arrays 假设我们有以下两个数组

Let's suppose this is called $array1 假设这称为$array1

Array
(
    [0] => Array
        (
            [Name] => Jack
            [Height] => 190
            [Shoe Size] => 40
        )

    [1] => Array
        (
            [Name] => Rose
            [Height] => 160
            [Shoe Size] => 52
        )

)

Suppose this is called $array2 假设这称为$array2

Array
(
    [0] => Name
    [1] => Shoe Size
)

Now, what I need to do, is to keep the keys in $array1 which are found in $array2 as values, so the output I'm expecting is something like this 现在,我需要做的,就是保持在键$array1它被发现在$array2的值,所以我期待的输出是这样的

Array
(
    [0] => Array
        (
            [Name] => Jack
            [Shoe Size] => 40
        )

    [1] => Array
        (
            [Name] => Rose
            [Shoe Size] => 52
        )

)

I tried array_intersect and array_intersect_key but they're both failing. 我尝试了array_intersect和array_intersect_key,但是它们都失败了。 does anyone have any idea how to do this? 有谁知道如何做到这一点?

What you need is array_intersect_key with array_flip 你需要的是array_intersect_keyarray_flip

$array3 = array_flip($array2);
foreach($array1 as &$a) {
   $a = array_intersect_key($a, $array3);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM