Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何合并具有相同值的数组_Php_Arrays - Fatal编程技术网

Php 如何合并具有相同值的数组

Php 如何合并具有相同值的数组,php,arrays,Php,Arrays,当我回显我的阵列时,它是这样的 [0] => Array ( [carriertype] => [radiotype] => RRUS 12 B2 [serialnumber] => CF82943634 [market] => Detroit Westland - DET 5 [bts] => 225011 [Host] => 225011_21

当我回显我的阵列时,它是这样的

 [0] => Array
    (
        [carriertype] => 
        [radiotype] => RRUS 12 B2
        [serialnumber] => CF82943634
        [market] => Detroit Westland - DET 5
        [bts] => 225011
        [Host] => 225011_21_MILE_and_ROMEO_PLANK
        [resourceId] => 0
        [type] => XMU
        [port] => 16
        [EP_AuxPlugInUnit_AuxPlugInUnitId] => RRU-7
        [userlabletext] => 225011_1_4
        [flag] => Yes
    )

[1] => Array
    (
        [carriertype] => 
        [radiotype] => RRUS 12 B2
        [serialnumber] => CF82961338
        [market] => Detroit Westland - DET 5
        [bts] => 225011
        [Host] => 225011_21_MILE_and_ROMEO_PLANK
        [resourceId] => 0
        [type] => XMU
        [port] => 15
        [EP_AuxPlugInUnit_AuxPlugInUnitId] => RRU-8
        [userlabletext] => 225011_2_4
        [flag] => Yes
    )

[2] => Array
    (
        [carriertype] => 
        [radiotype] => RRUS 12 B2
        [serialnumber] => CF82943628
        [market] => Detroit Westland - DET 5
        [bts] => 225011
        [Host] => 225011_21_MILE_and_ROMEO_PLANK
        [resourceId] => 0
        [type] => XMU
        [port] => 14
        [EP_AuxPlugInUnit_AuxPlugInUnitId] => RRU-9
        [userlabletext] => 225011_3_4
        [flag] => Yes
    )

[3] => Array
    (
        [radiotype] => 
        [carriertype] => 
        [serialnumber] => 
        [market] => Detroit Westland - DET 5
        [bts] => 225011
        [Host] => 225011_21_MILE_and_ROMEO_PLANK
        [resourceId] => 0
        [type] => XMU
        [port] => 1
        [EP_AuxPlugInUnit_AuxPlugInUnitId] => 
        [userlabletext] => ___B
        [flag] => Yes
    )

[4] => Array
    (
        [radiotype] => 
        [carriertype] => 
        [serialnumber] => 
        [market] => Detroit Westland - DET 5
        [bts] => 225011
        [Host] => 225011_21_MILE_and_ROMEO_PLANK
        [resourceId] => 0
        [type] => XMU
        [port] => 1
        [EP_AuxPlugInUnit_AuxPlugInUnitId] => 
        [userlabletext] => ___E
        [flag] => Yes
    )

[5] => Array
    (
        [carriertype] => AWS
        [radiotype] => RRUS 12 B4
        [serialnumber] => CF81666912
        [market] => Detroit Westland - DET 5
        [bts] => 225011
        [Host] => 225011_21_MILE_and_ROMEO_PLANK
        [resourceId] => 0
        [type] => XMU
        [port] => 16
        [EP_AuxPlugInUnit_AuxPlugInUnitId] => RRU-5
        [userlabletext] => 225011_2_2
        [flag] => Yes
    )

[6] => Array
    (
        [carriertype] => AWS
        [radiotype] => RRUS 12 B4
        [serialnumber] => CF81666918
        [market] => Detroit Westland - DET 5
        [bts] => 225011
        [Host] => 225011_21_MILE_and_ROMEO_PLANK
        [resourceId] => 0
        [type] => XMU
        [port] => 14
        [EP_AuxPlugInUnit_AuxPlugInUnitId] => RRU-6
        [userlabletext] => 225011_3_2
        [flag] => Yes
    )
但我希望数组合并具有相同的端口值。在上面的数组中,我有两个16、14和1端口号。所以它们应该用相同的值合并,如果某个值不同,那么应该用逗号保存。结果数组应该是

 [0] => Array
    (
        [carriertype] => AWS
        [radiotype] => RRUS 12 B2,RRUS 12 B4
        [serialnumber] => CF82943634,CF81666912
        [market] => Detroit Westland - DET 5
        [bts] => 225011
        [Host] => 225011_21_MILE_and_ROMEO_PLANK
        [resourceId] => 0
        [type] => XMU
        [port] => 16
        [EP_AuxPlugInUnit_AuxPlugInUnitId] => RRU-7,RRU-5
        [userlabletext] => 225011_1_4,225011_2_2
        [flag] => Yes
    )

[1] => Array
    (
        [carriertype] => 
        [radiotype] => RRUS 12 B2
        [serialnumber] => CF82961338
        [market] => Detroit Westland - DET 5
        [bts] => 225011
        [Host] => 225011_21_MILE_and_ROMEO_PLANK
        [resourceId] => 0
        [type] => XMU
        [port] => 15
        [EP_AuxPlugInUnit_AuxPlugInUnitId] => RRU-8
        [userlabletext] => 225011_2_4
        [flag] => Yes
    )

[2] => Array
    (
        [carriertype] => AWS
        [radiotype] => RRUS 12 B2,RRUS 12 B4
        [serialnumber] => CF82943628,CF81666918
        [market] => Detroit Westland - DET 5
        [bts] => 225011
        [Host] => 225011_21_MILE_and_ROMEO_PLANK
        [resourceId] => 0
        [type] => XMU
        [port] => 14
        [EP_AuxPlugInUnit_AuxPlugInUnitId] => RRU-9,RRU-6
        [userlabletext] => 225011_3_4,225011_3_2
        [flag] => Yes
    )

[3] => Array
    (
        [radiotype] => 
        [carriertype] => 
        [serialnumber] => 
        [market] => Detroit Westland - DET 5
        [bts] => 225011
        [Host] => 225011_21_MILE_and_ROMEO_PLANK
        [resourceId] => 0
        [type] => XMU
        [port] => 1
        [EP_AuxPlugInUnit_AuxPlugInUnitId] => 
        [userlabletext] => ___B,___E
        [flag] => Yes
    )
这应该是程序性的。请帮帮我

您可以尝试这种方法(请添加所需的验证):

您可以尝试此方法(请添加所需的验证):

试试这个

<?php

/**
 * @param array  $array
 * @param string $column
 *
 * @return array
 */
function mergeOnEquals(array $array, $column)
{
    $result = [];

    foreach ($array as $subArray) {
        $index = $subArray[$column];
        if (isset($result[$index])) {
            foreach($subArray as $key => $value) {
                if (!isset($result[$index][$key]) || $result[$index][$key] == '') {
                    $result[$index][$key] = $value;
                } elseif ($result[$index][$key] != $value) {
                    $result[$index][$key] .= ",$value";
                }
            }
        } else {
            $result[$index] = $subArray;
        }
    }

    return array_values($result);
}

$array = [
    [
        'carriertype'                      => null,
        'radiotype'                        => 'RRUS 12 B2',
        'serialnumber'                     => 'CF82943634',
        'market'                           => 'Detroit Westland - DET 5',
        'bts'                              => 225011,
        'Host'                             => '225011_21_MILE_and_ROMEO_PLANK',
        'resourceId'                       => 0,
        'type'                             => 'XMU',
        'port'                             => 16,
        'EP_AuxPlugInUnit_AuxPlugInUnitId' => 'RRU-7',
        'userlabletext'                    => '225011_1_4',
        'flag'                             => 'Yes',
    ],
    [
        'carriertype'                      => null,
        'radiotype'                        => 'RRUS 12 B2',
        'serialnumber'                     => 'CF82961338',
        'market'                           => 'Detroit Westland - DET 5',
        'bts'                              => 225011,
        'Host'                             => '225011_21_MILE_and_ROMEO_PLANK',
        'resourceId'                       => 0,
        'type'                             => 'XMU',
        'port'                             => 15,
        'EP_AuxPlugInUnit_AuxPlugInUnitId' => 'RRU-8',
        'userlabletext'                    => '225011_2_4',
        'flag'                             => 'Yes',
    ],
    [
        'carriertype'                      => null,
        'radiotype'                        => 'RRUS 12 B2',
        'serialnumber'                     => 'CF82943628',
        'market'                           => 'Detroit Westland - DET 5',
        'bts'                              => '225011',
        'Host'                             => '225011_21_MILE_and_ROMEO_PLANK',
        'resourceId'                       => 0,
        'type'                             => 'XMU',
        'port'                             => 14,
        'EP_AuxPlugInUnit_AuxPlugInUnitId' => 'RRU-9',
        'userlabletext'                    => '225011_3_4',
        'flag'                             => 'Yes',
    ],
    [
        'radiotype'                        => null,
        'carriertype'                      => null,
        'serialnumber'                     => null,
        'market'                           => 'Detroit Westland - DET 5',
        'bts'                              => 225011,
        'Host'                             => '225011_21_MILE_and_ROMEO_PLANK',
        'resourceId'                       => 0,
        'type'                             => 'XMU',
        'port'                             => 1,
        'EP_AuxPlugInUnit_AuxPlugInUnitId' => null,
        'userlabletext'                    => '___B',
        'flag'                             => 'Yes',
    ],
    [
        'radiotype'                        => null,
        'carriertype'                      => null,
        'serialnumber'                     => null,
        'market'                           => 'Detroit Westland - DET 5',
        'bts'                              => 225011,
        'Host'                             => '225011_21_MILE_and_ROMEO_PLANK',
        'resourceId'                       => 0,
        'type'                             => 'XMU',
        'port'                             => 1,
        'EP_AuxPlugInUnit_AuxPlugInUnitId' => null,
        'userlabletext'                    => '___E',
        'flag'                             => 'Yes',
    ],
    [
        'carriertype'                      => 'AWS',
        'radiotype'                        => 'RRUS 12 B4',
        'serialnumber'                     => 'CF81666912',
        'market'                           => 'Detroit Westland - DET 5',
        'bts'                              => 225011,
        'Host'                             => '225011_21_MILE_and_ROMEO_PLANK',
        'resourceId'                       => 0,
        'type'                             => 'XMU',
        'port'                             => 16,
        'EP_AuxPlugInUnit_AuxPlugInUnitId' => 'RRU-5',
        'userlabletext'                    => '225011_2_2',
        'flag'                             => 'Yes',
    ],
    [
        'carriertype'                      => 'AWS',
        'radiotype'                        => 'RRUS 12 B4',
        'serialnumber'                     => 'CF81666918',
        'market'                           => 'Detroit Westland - DET 5',
        'bts'                              => 225011,
        'Host'                             => '225011_21_MILE_and_ROMEO_PLANK',
        'resourceId'                       => 0,
        'type'                             => 'XMU',
        'port'                             => 14,
        'EP_AuxPlugInUnit_AuxPlugInUnitId' => 'RRU-6',
        'userlabletext'                    => '225011_3_2',
        'flag'                             => 'Yes',
    ],
];

print_r(mergeOnEquals($array, 'port'));
试试这个

<?php

/**
 * @param array  $array
 * @param string $column
 *
 * @return array
 */
function mergeOnEquals(array $array, $column)
{
    $result = [];

    foreach ($array as $subArray) {
        $index = $subArray[$column];
        if (isset($result[$index])) {
            foreach($subArray as $key => $value) {
                if (!isset($result[$index][$key]) || $result[$index][$key] == '') {
                    $result[$index][$key] = $value;
                } elseif ($result[$index][$key] != $value) {
                    $result[$index][$key] .= ",$value";
                }
            }
        } else {
            $result[$index] = $subArray;
        }
    }

    return array_values($result);
}

$array = [
    [
        'carriertype'                      => null,
        'radiotype'                        => 'RRUS 12 B2',
        'serialnumber'                     => 'CF82943634',
        'market'                           => 'Detroit Westland - DET 5',
        'bts'                              => 225011,
        'Host'                             => '225011_21_MILE_and_ROMEO_PLANK',
        'resourceId'                       => 0,
        'type'                             => 'XMU',
        'port'                             => 16,
        'EP_AuxPlugInUnit_AuxPlugInUnitId' => 'RRU-7',
        'userlabletext'                    => '225011_1_4',
        'flag'                             => 'Yes',
    ],
    [
        'carriertype'                      => null,
        'radiotype'                        => 'RRUS 12 B2',
        'serialnumber'                     => 'CF82961338',
        'market'                           => 'Detroit Westland - DET 5',
        'bts'                              => 225011,
        'Host'                             => '225011_21_MILE_and_ROMEO_PLANK',
        'resourceId'                       => 0,
        'type'                             => 'XMU',
        'port'                             => 15,
        'EP_AuxPlugInUnit_AuxPlugInUnitId' => 'RRU-8',
        'userlabletext'                    => '225011_2_4',
        'flag'                             => 'Yes',
    ],
    [
        'carriertype'                      => null,
        'radiotype'                        => 'RRUS 12 B2',
        'serialnumber'                     => 'CF82943628',
        'market'                           => 'Detroit Westland - DET 5',
        'bts'                              => '225011',
        'Host'                             => '225011_21_MILE_and_ROMEO_PLANK',
        'resourceId'                       => 0,
        'type'                             => 'XMU',
        'port'                             => 14,
        'EP_AuxPlugInUnit_AuxPlugInUnitId' => 'RRU-9',
        'userlabletext'                    => '225011_3_4',
        'flag'                             => 'Yes',
    ],
    [
        'radiotype'                        => null,
        'carriertype'                      => null,
        'serialnumber'                     => null,
        'market'                           => 'Detroit Westland - DET 5',
        'bts'                              => 225011,
        'Host'                             => '225011_21_MILE_and_ROMEO_PLANK',
        'resourceId'                       => 0,
        'type'                             => 'XMU',
        'port'                             => 1,
        'EP_AuxPlugInUnit_AuxPlugInUnitId' => null,
        'userlabletext'                    => '___B',
        'flag'                             => 'Yes',
    ],
    [
        'radiotype'                        => null,
        'carriertype'                      => null,
        'serialnumber'                     => null,
        'market'                           => 'Detroit Westland - DET 5',
        'bts'                              => 225011,
        'Host'                             => '225011_21_MILE_and_ROMEO_PLANK',
        'resourceId'                       => 0,
        'type'                             => 'XMU',
        'port'                             => 1,
        'EP_AuxPlugInUnit_AuxPlugInUnitId' => null,
        'userlabletext'                    => '___E',
        'flag'                             => 'Yes',
    ],
    [
        'carriertype'                      => 'AWS',
        'radiotype'                        => 'RRUS 12 B4',
        'serialnumber'                     => 'CF81666912',
        'market'                           => 'Detroit Westland - DET 5',
        'bts'                              => 225011,
        'Host'                             => '225011_21_MILE_and_ROMEO_PLANK',
        'resourceId'                       => 0,
        'type'                             => 'XMU',
        'port'                             => 16,
        'EP_AuxPlugInUnit_AuxPlugInUnitId' => 'RRU-5',
        'userlabletext'                    => '225011_2_2',
        'flag'                             => 'Yes',
    ],
    [
        'carriertype'                      => 'AWS',
        'radiotype'                        => 'RRUS 12 B4',
        'serialnumber'                     => 'CF81666918',
        'market'                           => 'Detroit Westland - DET 5',
        'bts'                              => 225011,
        'Host'                             => '225011_21_MILE_and_ROMEO_PLANK',
        'resourceId'                       => 0,
        'type'                             => 'XMU',
        'port'                             => 14,
        'EP_AuxPlugInUnit_AuxPlugInUnitId' => 'RRU-6',
        'userlabletext'                    => '225011_3_2',
        'flag'                             => 'Yes',
    ],
];

print_r(mergeOnEquals($array, 'port'));

我对if($result[$index][$key]!=”){$result[$index][$key]。=”,$value”}做了一些小改动,{$result[$index][$key]。=“$value”}……很有效……谢谢……非常有用。@DivyeshJesadiya我已经解决了答案中的问题。我对if($result[$index][$key]!=”){$result[$index][$key],$key],$value]做了一些小改动,{$result[$index][$key]。=”,$value”}[$key]。=“$value”;}…而且它很有效…谢谢…这非常有用。@DivyeshJesadiya我已经在答案中解决了这个问题。