Trying to Remove Nesting from Json Array in PHP(尝试在PHP中从Json数组中移除嵌套)
本文介绍了尝试在PHP中从Json数组中移除嵌套的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如您所见,我有一个具有多个元素的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);
?>
推荐答案
试试:
<?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!
这篇关于尝试在PHP中从Json数组中移除嵌套的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:尝试在PHP中从Json数组中移除嵌套
基础教程推荐
猜你喜欢
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01