PHP:将两个数组相交并获取两者的值

时间:2012-11-16 10:16:03

标签: php arrays intersection

所以我有点卡在这里,我可以用foreach循环来做,但我想找到一个更聪明的方法。

更新:我在问题中遗漏了一些东西。阵列可以以随机顺序并且具有不同的长度,因此具有不同的键。以下示例已更新。

情况如下:

数组1

array (
    slug1 => England,
    slug2 => France,
    slug3 => Italy,
    slug4 => Germany,
)

ARRAY2

array (
    slug2 => 215,
    slug1 => 168,
    slug4 => 55,
    slug5 => 149,
    slug3 => 40,
    slug6 => 137,
)

我希望与这些数组相交并构建一个具有以下元素的数组:

array (
    168 => England,
    215 => France,
    40 => Italy,
    55 => Germany,
)

注意:元素未订购,但可以轻松实现。

5 个答案:

答案 0 :(得分:2)

回答原始问题

你可以使用array_combine它通过一个数组用于键而另一个数组用于其值来创建一个数组

$array1 = array(
        "slug1" => "England",
        "slug2" => "France",
        "slug3" => "Italy",
        "slug4" => "Germany");

$array2 = array(
        "slug1" => "168",
        "slug2" => "215",
        "slug3" => "40",
        "slug4" => "55");

$final = array_combine($array2, $array1);

echo "<pre>";
print_r($final);

输出

Array
(
    [168] => England
    [215] => France
    [40] => Italy
    [55] => Germany
)

See Live Demo

回答更新的问题

Update: There was something that I've missed in the question. The arrays may come in random order and in different length thus having different keys. Examples below updated.

$array1 = array(
        "slug1" => "England",
        "slug2" => "France",
        "slug3" => "Italy",
        "slug4" => "Germany");

$array2 = array (
        "slug2" => 215,
        "slug1" => 168,
        "slug4" => 55,
        "slug5" => 149,
        "slug3" => 40,
        "slug6" => 137);



$final = customCombine($array2, $array1);

echo "<pre>";
print_r($final);

输出

Array
(
    [215] => France
    [168] => England
    [55] => Germany
    [40] => Italy
)

使用的功能

function customCombine($keys, $arr) {
    $t = array();
    foreach ( $keys as $k => $val ) {
        isset($arr[$k]) and $t[$val] = $arr[$k];
    }
    return $t;
}

答案 1 :(得分:2)

Baba的答案对你来说应该没问题,但这是处理两个阵列和/或不同大小之间不同关键顺序的有趣方法。

// get values from $array2 in the order in which they appear in $array1
// whereby the array keys are used to match them
$keys = array_intersect_key($array2, $array1);
// create a new array with the keys found in the previous step and 
// another array_intersect_key() to only select the matching items
// from $array1
array_combine($keys, array_intersect_key($array1, $keys));

它还确保array_combine()适用于相同大小的数组; $array2的大小是输出大小。

答案 2 :(得分:1)

要扩展Jack的答案,因为它可能按照构造它们的顺序组合数组,而不是键匹配的顺序:

Array
(
    [215] => England
    [168] => France
    [55] => Italy
    [40] => Germany
)

一些中间数据杂耍可以将其排序(无论哪个数组更短,它都可以工作):

$array1 = array(
    'slug1' => 'England',
    'slug2' => 'France',
    'slug3' => 'Italy',
    'slug4' => 'Germany'
);

$array2 = array (
    'slug2' => 215,
    'slug1' => 168,
    'slug4' => 55,
    'slug5' => 149,
    'slug3' => 40,
    'slug6' => 137
);

$keys = array_intersect_key($array2, $array1);
ksort($keys);
$intersect = array_intersect_key($array1, $keys);
ksort($intersect);
$final = array_combine($keys, $intersect);

print_r($final);

输出

Array
(
    [168] => England
    [215] => France
    [40] => Italy
    [55] => Germany
)

答案 3 :(得分:0)

使用array_combine()

<?php
$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);

print_r($c);
?>

http://in1.php.net/array-combine

答案 4 :(得分:0)

我找到了最适合我的解决方案:

http://eval.in/3578

<?php

$array1 = array("slug1" => "England","slug2" => "France","slug3" => "Italy","slug4" => "Germany");
$array2 = array("slug1" => "168","slug2" => "215","slug3" => "40","slug4" => "55", "slug5" => "178");

ksort($array1);
ksort($array2);

$test1 = array_intersect_key($array1, $array2);
$test2 = array_intersect_key($array2, $array1);

$final = array_combine($test2, $test1);
print_r($final);
?>