PHP通过两个多维数组的公共键进行匹配,并将两个数组与两个数组键合并

时间:2018-11-12 18:09:13

标签: php

晚上好,我有点问题。我有两个数组。喜欢

$firstArr = Array(
                [0] => Array(                   
                    [customer_id] => 11,
                    [home_delivery] => no,
            ),
                [1] => Array(
                    [customer_id] => 12,
                    [home_delivery] => no,
            ),
                [2] => Array(
                    [customer_id] => 13,
                    [home_delivery] => no,
            ),
        );

        $secondArr = Array(
                [0] => Array(
                    [customer_id] => 11,
                    [test] => no,
                    [is_active] => yes,
            ),
                [1] => Array(
                    [customer_id] => 22,
                    [test] => no,
                    [is_active] => yes,
            ),
        );

现在,我想获得第一个数组的customer_id与第二个数组的customer_id匹配的结果。标识两个数组的客户ID与第二个数组的值与第一个数组相加的值相同,否则该值为null。希望你们明白我的意思。我想要的输出如下所示。

$getResult = Array(
                [0] => Array(
                    [customer_id] => 11,
                    [home_delivery] => no,
                    [test] => no,
                    [is_active] => yes,
            ),
                [1] => Array(
                    [customer_id] => 12,
                    [home_delivery] => no,
                    [test] => '',
                    [is_active] => '',
            ),
                [2] => Array(
                    [customer_id] => 13,
                    [home_delivery] => no,
                    [test] => '',
                    [is_active] => '',
            ),
        );

我已经尝试过此代码,但是它不起作用。请帮助我。

$mergedArray = array();
        foreach ($firstArr as $index1 => $value1) {
            foreach ($secondArr as $index2 => $value2) {
                if ($array1[$index1]['customer_id'] == $array2[$index2]['customer_id']) {
                    $mergedArray[] = array_merge($firstArr[$index1], $secondArr[$index2]);
                }
            }
        }
echo "<pre>"; print_r($mergedArray); echo "</pre>";

2 个答案:

答案 0 :(得分:0)

您可以这样做:

<?php 

        $results = [];
        // Get all unique keys from both arrays
        $keys = array_unique(array_merge(array_keys($firstArr[0]), array_keys($secondArr[0])));

        // Make array of common customer_ids
        foreach (array_merge($firstArr, $secondArr) as $record) {
            $results[$record['customer_id']] = isset($results[$record['customer_id']]) ? array_merge($results[$record['customer_id']], $record) : $record;
        }
        // Fill keys which are not present with blank strings
        foreach ($keys as $key) {
            foreach ($results as $index => $result) {
                if(!array_key_exists($key, $result)){
                    $results[$index][$key] = '';
                }
            }
        }
        print_r($results);

答案 1 :(得分:0)

这就是我要做的:

$firstArr = array (
    0 => 
    array (
        'customer_id' => 11,
        'home_delivery' => 'no'
    ),
    1 => 
    array (
        'customer_id' => 12,
        'home_delivery' => 'no'
    ),
    2 => 
    array (
        'customer_id' => 13,
        'home_delivery' => 'no'
    )
);

$secondArr = array (
    0 => 
    array (
        'customer_id' => 11,
        'test' => 'no',
        'is_active' => 'yes'
    ),
    1 => 
    array (
        'customer_id' => 22,
        'test' => 'no',
        'is_active' => 'yes'
    )
);

$secondKey = array_column($secondArr,'customer_id');

foreach($firstArr as &$value){
    $idx2 = array_search($value['customer_id'], $secondKey);

    $value = array_merge($value, [
        'test' => false !== $idx2 ? $secondArr[$idx2]['test'] : '',
        'is_active' => false !== $idx2 ? $secondArr[$idx2]['is_active'] : '',
    ]);
}
print_r($firstArr);

输出:

Array
(
    [0] => Array
        (
            [customer_id] => 11
            [home_delivery] => no
            [test] => no
            [is_active] => yes
        )

    [1] => Array
        (
            [customer_id] => 12
            [home_delivery] => no
            [test] => 
            [is_active] => 
        )

    [2] => Array
        (
            [customer_id] => 13
            [home_delivery] => no
            [test] => 
            [is_active] => 
        )

)

Sandbox

我在这里使用2个“技巧”,第一个也是更重要的一个是array_column,它仅从数组中选择一列,但问题是结果数组中的键将与原始数组匹配数组。我们可以利用。

我们从数组列中获得的数组如下所示:

array (
   0 => 11,
   1 => 22
);

因为键与原始数组匹配,所以我们可以使用array_search(具有ID)来查找该键,然后可以在原始数组中使用它。这使我们可以通过展平第二个数组来“轻松”地搜索它。

因此,例如,当我们在上面的数组中查询$firstArr['customer_id'] = 11时,会得到密钥0(不是布尔值false,请参见下文)。然后,我们可以获取该索引并将其用于原始数组$secondArr,并从其他2列中获取值。

-请注意,数组搜索在找不到项目时会返回布尔值false,因为PHP将0和false视为相同,因此我们必须执行严格的类型检查!==而不是!=。否则,PHP会将0索引与false混淆,这不是我们想要的。

第二个“技巧”是在foreach值中使用&,这是通过引用允许我们直接修改循环中使用的数组的。这是可选的,因为您可以轻松地创建一个新数组。但我想我会把它作为一种选择。