在 PHP 中合并二维 json 数组

Merging a 2 dimentional json array in PHP

我有两个 JSON 数组,每个数组中有多个元素。有人可以帮助合并这两个数组,以便将第二个数组元素与第一个数组分别添加到它们的索引中。

第一个 JSON 数组:

$json1 =
    '[
    {
    "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":""     
    },
    {
    "CHEPTEL":"12001",
    "NOM":"La Ferme ",
    "CODE_POSTAL":"12630",
    "VILLE":"AGEN D AVEYRON",
    "LATITUDE":"44.343518",
    "LONGITUDE":"2.716004",
    "DESCRIPTIF_FERME":""
    }
    ]';
    

第二个 JSON 数组:

$json2= '[
    {
    "DEMA_CODE":"08-93",
    "ANNEE_ADHESION":"2016",
    "RACE_MERES":"Limousine",
    "DESCRIPTIF_ATELIER":""
    },
    {
    "DEMA_CODE":"08-93",
    "ANNEE_ADHESION":"2016",
    "RACE_MERES":"Limousine",
    "DESCRIPTIF_ATELIER":""
    }
    ]';
    

最后我想让数组变成这样:

$newarray= '[
{
"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":"",  
"DESCRIPTIF_ATELIER":""
"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":""
"DEMA_CODE":"08-93",
"ANNEE_ADHESION":"2016",
"RACE_MERES":"Limousine",
"DESCRIPTIF_ATELIER":""
}
]';
$finalArray = [];

foreach($json1 as $key => $obj){
    $newObj = array_merge($obj, $json2[$key]); 
    array_push($finalArray, $newObj);
}

you will get merged array in `$finalArray`