让我们假设我们有以下两个数组
我们假设这叫做$array1
Array
(
[0] => Array
(
[Name] => Jack
[Height] => 190
[Shoe Size] => 40
)
[1] => Array
(
[Name] => Rose
[Height] => 160
[Shoe Size] => 52
)
)
假设这被称为$array2
Array
(
[0] => Name
[1] => Shoe Size
)
现在,我需要做的是将$array1
中的密钥保存在$array2
中作为值,因此我期望的输出是这样的
Array
(
[0] => Array
(
[Name] => Jack
[Shoe Size] => 40
)
[1] => Array
(
[Name] => Rose
[Shoe Size] => 52
)
)
我尝试了array_intersect和array_intersect_key,但它们都失败了。有没有人知道如何做到这一点?
答案 0 :(得分:2)
您需要array_intersect_key
array_flip
$array3 = array_flip($array2);
foreach($array1 as &$a) {
$a = array_intersect_key($a, $array3);
}