在此示例中,如何解决JSON的数组合并?

时间:2018-10-05 10:49:50

标签: php arrays json array-merge

我解析Excel工作表并获取此JSON:

[
   {
      "A":"Samsung",
      "Groupe":{
         "F":"TV",
         "D":"HDR"
      }
   },
   {
      "A":null,
      "Groupe":{
         "F":null,
         "D":null
      }
   },
   {
      "A":"Sony",
      "Groupe":{
         "F":"T.V",
         "D":"LCD"
      }
   },
   {
      "A":"Sony",
      "Groupe":{
         "F":"PS4",
         "D":"Pro edition"
      }
   },
   {
      "A":"Sony",
      "Groupe":{
         "F":"Smart Phone",
         "D":"Quad core"
      }
   }
]

Php代码:

$data = [];
for ($row = 15; $row <= 25; $row++) {
    $data[] = [
        'A' => $worksheet->getCell('A'.$row)->getValue(),
        'Groupe' => [
            'F' => $worksheet->getCell('F'.$row)->getValue(),
            'D' => $worksheet->getCell('D'.$row)->getValue()
        ]
    ];
} 

如何根据"A"组织(排序)json?
我尝试了此操作,但仍然无法将相同的"Groupe"的{​​{1}}合并到一起:

  1. 带走NULL列。
  2. 创建阵列的副本。
  3. 重新组合新数组中相同元素的字段(这不起作用)

代码:

"A"

编辑: 我为$ data1获得的结果与输入JSON完全相同(除了删除了NULL),因此看起来merge Array无法正常工作,我需要的是:

$data1  = [];
for ($l = 0; $l < count($data); $l++){
    $data1[$l] = $data[$l];
}

for ($j = 0; $j < count($data); $j++) {

    if($data[$j]['A'] != NULL){

        if($data[$j]['A'] !== $data[$j+1]['A']){
            $data1[$j] = $data[$j];
        } 
        else{
            $data1[$j]['A']= $data[$j]['A'];
            $data1[$j]['Groupe']= array_merge($data[$j]['Groupe'], $data[$j+1]['Groupe']);

        }
    }
}

此外,它还向我展示了此内容:

  

注意:未定义的偏移量:在线C:\ xampp \ htdocs \ phptoexcel.php中为11   43    第43行: [ { "A":"Samsung", "Groupe":{ "F":"TV", "D":"HDR" } }, { "A":"Sony", "Groupe": [{ "F":"T.V", "D":"LCD" },{ "F":"PS4", "D":"Pro edition" }, {"F":"Smart Phone", "D":"Quad core" }] }]

3 个答案:

答案 0 :(得分:0)

使用A值作为$data中的键,以便您可以按其分组:

$data = [];
for ($row = 15; $row <= 25; $row++) {
    //get A value, skip if A = NULL
    $a = $worksheet->getCell('A'.$row)->getValue(),
    if($a===NULL)continue;

    //get F and D VALUE, skip if one of them = NULL
    $f = $worksheet->getCell('F'.$row)->getValue();
    $d = $worksheet->getCell('D'.$row)->getValue();
    if($f===null || $d===null)continue;

    //test if A is a key in $data. If not, create
    if(!array_key_exist( $a, $data ){
          $data[$a]=[
             'A'=>$a,
             'Groupe'=>[]
             ];
           }

    //Put F and D in a new array in Groupe
    $data[$a]['Groupe'][]=["F"=>$f,"D"=>$d];
} 

您最终将获得:

$data=>     
[ "Samsung" =>[ "A" => "Samsung",
                "Groupe" => [ 0 =>[ "F" => "TV",
                                    "D" => "HDR"
                                  ]
                            ]
              ],
  "Sony" =>  [ "A" => "Sony",
               "Groupe" => [ 0 =>[ "F":"TV",
                                   "D":"HDR"
                                 ],
                             1 =>[ "F":"T.V",
                                   "D":"LCD"
                                 ],
                             2 =>[ "F":"PS4",
                                   "D":"Pro edition"
                                 ],
                             3 =>[ "F":"Smart Phone",
                                   "D":"Quad core"
                                 ],

            ]
]

答案 1 :(得分:0)

尝试

$arrUnique = array();
$result = array();
$i=0;
foreach($data as $value){
 if($value['A']!=null){
    $data1  = [];
    $intID = $value['A'];
    if( in_array( $intID, $arrUnique ) ) {
        $key = array_search ($intID, $arrUnique);
        $result[$key]['Groupe'][] = $value['Groupe'];

    }else{
        $data1['A'] = $value['A'];
        $data1['Groupe'][] = $value['Groupe'];
        $result[$i]=$data1;
        $arrUnique[]=$value['A'];
        $i++;
    }
}

}

答案 2 :(得分:0)

我通常不使用PHP而是使用jq command line实用工具执行JSON到JSON的转换。

鉴于您输入的JSON文件,您可以使用以下jq过滤器:

jq '[[sort_by(.A)|.[]|select(.A!=null)]|group_by(.A)|.[]as $i|{A:$i[].A,Groupe:$i|map(.Groupe)}]|unique' file
[
  {
    "A": "Samsung",
    "Groupe": [
      {
        "F": "TV",
        "D": "HDR"
      }
    ]
  },
  {
    "A": "Sony",
    "Groupe": [
      {
        "F": "T.V",
        "D": "LCD"
      },
      {
        "F": "PS4",
        "D": "Pro edition"
      },
      {
        "F": "Smart Phone",
        "D": "Quad core"
      }
    ]
  }
]