0

有没有办法做到以下几点:

$array1 = array( "two" => "2", "three" => "3")

$array2 = array("two", "three", "four")

我想将array2' 值与array1' 键匹配。匹配后,我想输出array1的值。

谢谢

4

2 回答 2

3

就像马克贝克评论的那样,你可以array_flip()一起使用array_intersect_key()

$array1 = array( "two" => "2", "three" => "3");

$array2 = array("two", "three", "four");
$array2 = array_flip($array2);

print_r(array_intersect_key($array1, $array2) );

输出:

Array
(
    [two] => 2
    [three] => 3
)
于 2013-07-30T11:06:11.267 回答
1
$array1 = array( "two" => "2", "three" => "3");
foreach($array1 as $key=>$val){
    $array_1[] = $key;
}
$array2 = array("two", "three", "four");


$result = array_diff($array2, $array_1);

print_r($result);
于 2013-07-30T10:59:20.457 回答