2016-10-20 13 views
0

私は私の配列をエコーするとき、それはこの同じ値を持つ配列をマージする方法を

[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 
    ) 

のように見えますが、私は、配列が同じポート値をマージします。上記の配列には、2つの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 
    ) 

これはプログラムで行う必要があります。私を助けてください。

答えて

1

この

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

Live demoを試してみてください。

+0

if($ result [$ index] [$ key]!= ""){$ result [$ index] [$ key]。= "、$ value";} else {$ result [ $ index] [$ key]。= "$ value";} ...それは動作します...ありがと...非常に役に立ちます。 –

+0

@DivyeshJesadiya私は答えで問題を修正しました。 – wazelin

1

あなたは(必要な検証を追加してください)この方法を試すことができます。

$myArray = your main array; 

foreach($myArray as $k=>$data) 
{ 
    $port[$data["port"]][]= $k ; 

} 
$key = array("carriertype", "radiotype" , "serialnumber", 
    "market", "bts" , "Host" , "resourceId", "type" , "port" , "EP_AuxPlugInUnit_AuxPlugInUnitId", "userlabletext" , "flag"); 


$ret = array(); 
foreach($port as $arrdata) 
{ 
    $retArr = array(); 
    foreach($arrdata as $data) 
    { 

     foreach($key as $k) 
     { 
      if($k !='flag' || $k !='port') 
      { 
       $retArr[$k]= !empty($retArr[$k]) ? $retArr[$k].",".$a[$data][$k] : $a[$data][$k]; 
      } 
      else 
      { 
       $retArr[$k]= $a[$data][$k]; 
      } 

     } 

    } 
    $ret[] = $retArr; 

} 
関連する問題