2

我有两个数组,比方说

$array1 = array(0 => 10, 1 => 21, 2 => 34, 'somekey' => 45, 'otherkey' => 15);
$array2 = array(0 => 9, 1 => 10, 2 => 14, 'otherkey' => 15, 'somekey' => 43);

我需要返回一个数组,其中仅包含两个数组包含的值,与它们的键无关。在这种情况下,结果数组将在键 0 处包含值 10,在键 1 处包含值 15

4

2 回答 2

6

使用array_intersect()

$array3 = array_intersect( $array1, $array2);

如果您想摆脱键,请通过以下方式运行该数组array_values()

$array3 = array_values( $array3);

将设置 $array3为:

Array
(
    [0] => 10
    [1] => 15
)
于 2012-07-26T19:06:59.500 回答
1
    <?php

    $array1 = array(0 => 10, 1 => 21, 2 => 34, 'somekey' => 45, 'otherkey' => 15);
    $array2 = array(0 => 9, 1 => 10, 2 => 14, 'otherkey' => 15, 'somekey' => 43);

    $array1 = array_values($array1);
    $array2 = array_values($array2);
    $array3 = array_merge($array1,$array2);

    echo '<pre>';
    print_r($array3);
    echo '</pre>';

?>
于 2012-07-26T19:19:45.233 回答