尝试从 PHP 中的 Json 数组中删除嵌套

Trying to Remove Nesting from Json Array in PHP

如您所见,我有一个包含多个元素的 json 数组,我一直在尝试删除嵌套(“DEMARCHE”)元素,并且我希望将数组展平。但它只提供一个输出数组,但我的 json 文件包含多个数组。 如果有人能帮助我,我真的很感激。 _ 谢谢

** 这是输入 **

[
    {
    "CHEPTEL":"12001116",
    "NOM":"La Ferme de Jean-Marc et Aurélien ",
    "CODE_POSTAL":"12630",
    "VILLE":"AGEN D AVEYRON",
    "LATITUDE":"44.343518",
    "LONGITUDE":"2.716004",
    "DESCRIPTIF_FERME":"",
    "DEMARCHE" : [
    {
    "DEMA_CODE":"08-93",
    "ANNEE_ADHESION":"2016",
    "RACE_MERES":"Limousine",
    "DESCRIPTIF_ATELIER":""
    }
    ]        
    },
    {
    "CHEPTEL":"12001",
    "NOM":"La Ferme ",
    "CODE_POSTAL":"12630",
    "VILLE":"AGEN D AVEYRON",
    "LATITUDE":"44.343518",
    "LONGITUDE":"2.716004",
    "DESCRIPTIF_FERME":"",
    "DEMARCHE" : [
    {
    "DEMA_CODE":"08-93",
    "ANNEE_ADHESION":"2016",
    "RACE_MERES":"Limousine",
    "DESCRIPTIF_ATELIER":""
    }
    ]        
    }
    ]

** 我希望输出为 **

[
    {
    "CHEPTEL":"12001116",
    "NOM":"La Ferme de Jean-Marc et Aurélien ",
    "CODE_POSTAL":"12630",
    "VILLE":"AGEN D AVEYRON",
    "LATITUDE":"44.343518",
    "LONGITUDE":"2.716004",
    "DESCRIPTIF_FERME":"",
    "DEMA_CODE":"08-93",
    "ANNEE_ADHESION":"2016",
    "RACE_MERES":"Limousine",
    "DESCRIPTIF_ATELIER":""     
    },
    {
    "CHEPTEL":"12001",
    "NOM":"La Ferme ",
    "CODE_POSTAL":"12630",
    "VILLE":"AGEN D AVEYRON",
    "LATITUDE":"44.343518",
    "LONGITUDE":"2.716004",
    "DESCRIPTIF_FERME":"",
    "DEMA_CODE":"08-93",
    "ANNEE_ADHESION":"2016",
    "RACE_MERES":"Limousine",
    "DESCRIPTIF_ATELIER":""       
    }
    ]

** 我的代码在这里 **

<?php
   
    
    $array1 = json_decode($json,true);
    
    
    function array_flatten($array) {
    
       $return = array();
       foreach ($array as $key => $value) {
           if (is_array($value))
           { $return = array_merge($return, array_flatten($value));
                             }
           else {$return[$key] = $value;
                }
       }
       return $return;
    
    }
    
    $array  = $array1;
    
    $result = array_flatten($array);
    
    $jsonnew = json_encode($result);
    
    var_dump($jsonnew);
    
    
    ?>

只需循环并使用参考即可。

//& sign in this context is called a reference.
//That means the $original will be changed if $values is changed inside the loop.
foreach($original as &$values) {

    $demarche = $values['DEMARCHE'];
    unset($values['DEMARCHE']);

    $values['DEMA_CODE'] = $demarche['DEMA_CODE'];
    $values['ANNEE_ADHESION'] = $demarche['ANNEE_ADHESION'];
    $values['RACE_MERES'] = $demarche['RACE_MERES'];
    $values['DESCRIPTIF_ATELIER'] = $demarche['DESCRIPTIF_ATELIER'];

    
}

结果:

[
   {
      "CHEPTEL":"12001116",
      "NOM":"La Ferme de Jean-Marc et Aur\u00e9lien ",
      "CODE_POSTAL":"12630",
      "VILLE":"AGEN D AVEYRON",
      "LATITUDE":"44.343518",
      "LONGITUDE":"2.716004",
      "DESCRIPTIF_FERME":"",
      "DEMA_CODE":"08-93",
      "ANNEE_ADHESION":"2016",
      "RACE_MERES":"Limousine",
      "DESCRIPTIF_ATELIER":""
   },
   {
      "CHEPTEL":"12001",
      "NOM":"La Ferme ",
      "CODE_POSTAL":"12630",
      "VILLE":"AGEN D AVEYRON",
      "LATITUDE":"44.343518",
      "LONGITUDE":"2.716004",
      "DESCRIPTIF_FERME":"",
      "DEMA_CODE":"08-93",
      "ANNEE_ADHESION":"2016",
      "RACE_MERES":"Limousine",
      "DESCRIPTIF_ATELIER":""
   }
]

试试这个:

<?php
  $json = '[
    {
      "CHEPTEL":"12001116",
      "NOM":"La Ferme de Jean-Marc et Aurélien ",
      "CODE_POSTAL":"12630",
      "VILLE":"AGEN D AVEYRON",
      "LATITUDE":"44.343518",
      "LONGITUDE":"2.716004",
      "DESCRIPTIF_FERME":"",
      "DEMARCHE" : [
        {
        "DEMA_CODE":"08-93",
        "ANNEE_ADHESION":"2016",
        "RACE_MERES":"Limousine",
        "DESCRIPTIF_ATELIER":""
        }
      ]        
    },
    {
      "CHEPTEL":"12001",
      "NOM":"La Ferme ",
      "CODE_POSTAL":"12630",
      "VILLE":"AGEN D AVEYRON",
      "LATITUDE":"44.343518",
      "LONGITUDE":"2.716004",
      "DESCRIPTIF_FERME":"",
      "DEMARCHE" : [
        {
        "DEMA_CODE":"08-93",
        "ANNEE_ADHESION":"2016",
        "RACE_MERES":"Limousine",
        "DESCRIPTIF_ATELIER":""
        }
      ]        
    }
    ]';
    
  $array1 = json_decode($json, true);
  $newArr = array_map(function ($arr) {
    foreach ($arr as $key => $val) {
      if ($key === 'DEMARCHE') {
        foreach ($val[0] as $key1 => $val1) {
          $arr[$key1] = $val1;
        }
      }
    }
    
    unset($arr['DEMARCHE']);
    
    return $arr;
  }, $array1);
  
  echo json_encode($newArr, JSON_PRETTY_PRINT); // done!