值匹配时组合2个数组

时间:2018-11-21 06:34:17

标签: php html arrays

所以我有这些数组是从2个不同的数据库中提取的,当 ['item_name'] ['name'] 时,我想将它们组合成一个数组strong>被匹配,然后从 Array2

获得 ['id']

我尝试执行in_array,但是由于它是多维的,所以我无法获得所需的正确输出,我也尝试了foreach,但是我也无法获得正确的输出,或者我做错了,我我不知道该怎么做我想要的输出。

Array1示例:

Array
(
    [0] => Array
        (
            [item_id] => 1
            [item_name] => Bag
            [Color] => Purple
        )

    [1] => Array
        (
            [item_id] => 2
            [item_name] => Pencil
            [Color] => Yellow
        )

    [2] => Array
        (
            [item_id] => 3
            [item_name] => Tumbler
            [Color] => Blue
        )

    [3] => Array
        (
            [item_id] => 4
            [item_name] => Shirt
            [Color] => Red
        )

)

Array2示例:

Array
(
    [0] => Array
        (
            [id] => 11
            [name] => Bag
        )

    [1] => Array
        (
            [id] => 22
            [name] => Pencil
        )

    [2] => Array
        (
            [id] => 33
            [name] => Tumbler
        )

    [3] => Array
        (
            [id] => 44
            [name] => Shirt
        )

    [4] => Array
        (
            [id] => 55
            [name] => Paper
        )

    [5] => Array
        (
            [id] => 66
            [name] => Chair
        )

    [6] => Array
        (
            [id] => 4
            [name] => Notebook
        )

)

所以我的预期输出是:

Array
(
    [0] => Array
        (
            [id] => 11
            [name] => Bag
            [Color] => Purple
        )

    [1] => Array
        (
            [id] => 22
            [name] => Pencil
            [Color] => Yellow
        )

    [2] => Array
        (
            [id] => 33
            [name] => Tumbler
            [Color] => Blue
        )

    [3] => Array
        (
            [id] => 44
            [name] => Shirt
            [Color] => Red
        )
)

3 个答案:

答案 0 :(得分:0)

尝试使用foreach循环。 检查是否在foreach循环中是否有任何索引值与另一个数组匹配,然后使用所有这些值创建一个新数组。 您可以在此处使用counter来获取循环内数组的值。

答案 1 :(得分:0)

您可以使用第二个数组中的名称和ID创建一个映射,然后循环修改第一个数组。

考虑以下代码:

$a1 = array("item_id" => 1, "item_name" => "Bag", "Color" => "Purple");
$a2 = array("item_id" => 2, "item_name" => "Pencil", "Color" => "Yellow");
$a3 = array("item_id" => 3, "item_name" => "Tumbler", "Color" => "Blue");
$b1 = array("item_id" => 11, "item_name" => "Bag");
$b2 = array("item_id" => 22, "item_name" => "Pencil");
$b3 = array("item_id" => 33, "item_name" => "Tumbler");
$arr1 = array($a1, $a2, $a3);
$arr2 = array($b1, $b2, $b3);

$map = []; // here keys are names and value are the id 
foreach($arr2 as $elem)
    $map[$elem["item_name"]] = $elem["item_id"];


$ans = [];
foreach ($arr1 as $elem) {
    if (array_key_exists($elem["item_name"], $map))  {
        $elem["item_id"] = $map[$elem["item_name"]];
        $ans[] = $elem;
    }
}

echo print_r($ans);

这样,如果尝试嵌套的for循环,则可以实现O(n)而不是O(n^2)的复杂性

答案 2 :(得分:0)

感谢anju和David Winder,我尝试早些简化我的代码,我想我明白了,如果有人对我的代码感兴趣,这是这样的:

index = 0;
foreach ($array1 as $val){
    foreach ($array2 as $val2){
        if ($val['item_id'] == $val2['id']){
            $filtered[$index]['id'] = $val2['id'];
            $filtered[$index]['name'] = $val2['name'];
            $filtered[$index]['color'] = $val['color'];
            $index++;
        }
    }
}