简体   繁体   中英

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.

$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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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