douli1872 2018-11-12 18:09
浏览 50

PHP通过两个多维数组的公共密钥匹配并将两个数组合并为两个数组键[duplicate]

This question already has an answer here:

Good evening, I have a little bit problem. I have two array. like

$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,
            ),
        );

Now i want to get the result like first array's customer_id match with the second array customer_id. Id two array's customer id is same the the value of second array add with first array otherwise the value will be null. Hope guys you got my point what i want. The output which i want is like the below.

$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] => '',
            ),
        );

I have tried by this code, but it doesnt work. Please help me.

$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>";
</div>
  • 写回答

2条回答 默认 最新

  • dongzhonggua4229 2018-11-12 18:39
    关注

    You can do this :

    <?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);
    
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP