3

我在类似的问题上看到了另一个问题,但我的问题似乎很明显。

我有两个数组:

阵列#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
)

我玩过各种数组函数,但似乎无法弄清楚,任何提示将不胜感激,谢谢!

4

1 回答 1

2
<?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"
}
于 2013-09-08T23:37:18.110 回答