dqfwcj0030 2019-06-12 06:18
浏览 117

php合并基于匹配列名的数组与多个条目或重复项

I have an array of arrays like this :

$data = array (
    'data1' => array (
        0 => 
        array (
            0 => 'ID',
            1 => 'PinCode',
            2 => 'Date',
            ),
        1 => 
        array (
            0 => '101',
            1 => '454075',
            2 => '2012-03-03',
            ),
        2 => 
        array (
            0 => '103',
            1 => '786075',
            2 => '2012-09-05',
            ),
        ),
    'data2' => array (
        0 => 
        array (
            0 => 'Balance',
            1 => 'ID',
            ),
        1 => 
        array (
            0 => '4533',
            1 => '101',
            )
        ),
    'data3' => array (
        0 => 
        array (
            0 => 'Active',
            1 => 'ID',
            ),
        1 => 
        array (
            0 => 'Yes',
            1 => '101',
            ),
        2 => 
        array (
            0 => 'No',
            1 => '103',
            )
        ),
    );

Here is the current working answer by user @Nigel Ren I have to this question here.

$store = [];
$headers = [];
foreach ( $data as $set )  {
    $headerRow = array_shift($set);
    // Collect all header columns
    $headers = array_merge($headers, $headerRow);
    foreach ( $set as $index => $list ){
        // Create associative list of data so they can be combined (i.e. ID fields)
        $list = array_combine($headerRow, $list);
        // Use ID value as key and create if needed
        if ( !isset($store[$list["ID"]]) )    {
            $store[$list["ID"]] = $list;
        }
        else    {
            $store[$list["ID"]] = array_merge($store[$list["ID"]], $list);
        }
    }
}

$headers = array_unique($headers);
$output = [ 'output' => [$headers]];
// Create template array, so that missing fields will be set to null
$blank = array_fill_keys($headers, null);
foreach ( $store as $dataRow )  {
    // Fill in the fields for this ID and then change to numeric keys
    $output['output'][] = array_values(array_merge($blank, $dataRow));
}
print_r($output);

Above code works perfectly for the above given array.

Here are the new cases I need to handle:

CASE 1 : When the data contains only one array where the ID are same :

$data = array (
    'data1' => array (
        0 =>
        array (
            0 => 'ID',
            1 => 'PinCode',
            2 => 'Date',
            ),
        1 =>
        array (
            0 => '101',
            1 => '454075',
            2 => '2012-03-03',
            ),
        2 =>
        array (
            0 => '101',
            1 => '786075',
            2 => '2012-09-05',
            ),
        ),
    'data2' => array (
        0 =>
        array (
            0 => 'Balance',
            1 => 'ID',
            ),
        ),
'data3' => array (
    0 =>
    array (
        0 => 'Active',
        1 => 'ID',
        ),
    ),
    );

In this case Nigel's answer doesn't output both the both records for the same ID. It outputs just a single record. Expected output when there's a single array with duplicate ID's present :

Array
(
    [output] => Array
        (
            [0] => Array
                (
                    [0] => ID
                    [1] => PinCode
                    [2] => Date
                )

            [1] => Array
                (
                    [0] => 101
                    [1] => 454075
                    [2] => 2012-03-03
                )

            [2] => Array
                (
                    [0] => 101
                    [1] => 786075
                    [2] => 2012-09-05
                )

        )

)

CASE 2 :

When any of the array's other than the first array contain's entries for multiple same ID.

$data = array (
    'data1' => array (
        0 =>
        array (
            0 => 'ID',
            1 => 'PinCode',
            2 => 'Date',
            ),
        1 =>
        array (
            0 => '101',
            1 => '454075',
            2 => '2012-03-03',
            ),
        2 =>
        array (
            0 => '103',
            1 => '786075',
            2 => '2012-09-05',
            ),
        ),
    'data2' => array (
        0 =>
        array (
            0 => 'Order',
            1 => 'ID',
            ),
        1 =>
        array (
            0 => 'Colgate',
            1 => '101',
            ),
        2 =>
        array (
            0 => 'Waffles',
            1 => '101',
            )
        ),
    );

Expected output in this case :

Array
(
    [output] => Array
        (
            [0] => Array
                (
                    [0] => ID
                    [1] => PinCode
                    [2] => Date
                    [3] => Order
                )

            [1] => Array
                (
                    [0] => 101
                    [1] => 454075
                    [2] => 2012-03-03
                    [3] => Colgate
                )
            [2] => Array
                (
                    [0] => 101
                    [1] => 454075
                    [2] => 2012-03-03
                    [3] => Waffles
                )

            [3] => Array
                (
                    [0] => 103
                    [1] => 786075
                    [2] => 2012-09-05
                    [3] =>
                )

        )

)

How do I do it?

  • 写回答

1条回答 默认 最新

  • doumeng9188 2019-06-12 09:40
    关注

    you can get the desired output by using a loop:

    $desired_array = array();
    
    $n_data1 = count($data['data1']);
    $n_data2 = count($data['data2']);
    $n_data3 = count($data['data3']);
    
    for ($i=0; $i < $n_data1; $i++) {
    
     if ($n_data2 > $i) {
        foreach ($data['data2'][$i] as $key => $value) {
            if(!in_array($value,$data['data1'][$i])){
                $data['data1'][$i][]=$value;
            }
        }
     } else {
        $data['data1'][$i][]= null;
     }
    
    }
    
    for ($i=0; $i < $n_data1; $i++) {
    
     if ($n_data3 > $i) {
        foreach ($data['data3'][$i] as $key => $value) {
    
            if(!in_array($value,$data['data1'][$i])){
                $data['data1'][$i][]=$value;
            }
        }
     } else {
       $data['data1'][$i][]= null; 
     }
    }
    
    $desired_array = $data['data1'];
    
    评论

报告相同问题?

悬赏问题

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