本文介绍了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',
            )
        ),
    );

这是用户@Nigel Ren的当前工作答案,我必须对此在这里.

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:

案例1:当数据仅包含一个ID相同的数组时:

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

在这种情况下,Nigel的答案不会同时输出具有相同ID的两条记录.它仅输出一条记录.当存在具有重复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
                )

        )

)

案例2:

除第一个数组以外的任何其他数组包含多个相同ID的条目.

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] =>
                )

        )

)

我该怎么做?

推荐答案

您可以使用循环获得所需的输出:

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

这篇关于php根据具有多个条目或重复项的匹配列名称合并数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 07:13