简体   繁体   English

php根据具有多个条目或重复项的匹配列名称合并数组

[英]php merge arrays based on matching column name with multiple entries or duplicates

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. 这是用户@Nigel Ren的当前工作答案,我在这里对此有疑问

$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 : 情况1:当数据仅包含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 => '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. 在这种情况下,Nigel的答案不会同时输出相同ID的两条记录。 It outputs just a single record. 它仅输出一条记录。 Expected output when there's a single array with duplicate ID's present : 当存在具有重复ID的单个数组时的预期输出:

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 : 案例2:

When any of the array's other than the first array contain's entries for multiple same ID. 当除第一个数组以外的任何其他数组包含多个相同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? 我该怎么做?

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'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM