我看到另一个关于类似问题的问题,但我的问题似乎有所不同。
我有两个数组:
数组#1:
Array
(
[1] => Baby/Juvenile
[2] => Bedding
[3] => Room Decor
[4] => Bath & Potty
[5] => Feeding
[7] => Furniture
[8] => Bath
[9] => Towels
[10] => Shower Curtains
)
数组#2
Array
(
[1] => 5
[2] => 7
[3] => 9
)
我想比较这些数组并根据第二个数组中的值从第一个数组中提取键/值对。我想要的输出是:
Array
(
[5] => Feeding
[7] => Furniture
[9] => Towels
)
我尝试了各种数组函数,但似乎无法解决这个问题,非常感谢任何提示,谢谢!
最佳答案
<?php
$array_one = array
(
'1' => 'Baby/Juvenile',
'2' => 'Bedding',
'3' => 'Room Decor',
'4' => 'Bath & Potty',
'5' => 'Feeding',
'7' => 'Furniture',
'8' => 'Bath',
'9' => 'Towels',
'10' => 'Shower Curtains'
);
$array_two = array
(
'1' => 5,
'2' => 7,
'3' => 9
);
foreach($array_two as $value)
{
$result[$value] = $array_one[$value];
}
var_dump($result);
?>
会输出
array(3) {
[5]=>
string(7) "Feeding"
[7]=>
string(9) "Furniture"
[9]=>
string(6) "Towels"
}
关于php - 使用两个数组,其中一个数组的值与另一个数组的键匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18689250/