我有两个数组,比方说
$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
$array3 = array_intersect( $array1, $array2);
如果您想摆脱键,请通过以下方式运行该数组array_values()
:
$array3 = array_values( $array3);
这将设置 $array3
为:
Array
(
[0] => 10
[1] => 15
)
<?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>';
?>