'Trying to Remove Nesting from Json Array in PHP

As you can see i have a json array with multiple elements, and I have been trying to remove the nesting ("DEMARCHE") element and I want the array to be flattened. but It is only giving a single output array, but my json file consists more than 1 array. I would really be thankful if someone can help me. _ Thank you

** This is the input **

[
    {
    "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":""
    }
    ]        
    }
    ]

** And I want the output to be **

[
    {
    "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":""       
    }
    ]

** My code is here **

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


Solution 1:[1]

Just loop over and use a reference.

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

    
}

Result:

[
   {
      "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":""
   }
]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1