Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php json输出中的MongoDB对象id问题_Php_Json_Mongodb_Phalcon - Fatal编程技术网

Php json输出中的MongoDB对象id问题

Php json输出中的MongoDB对象id问题,php,json,mongodb,phalcon,Php,Json,Mongodb,Phalcon,我必须通过PHP从MongoDB读取一些数据。 当我将MongoDB结果转换为Json时,得到如下结果: { "records": [ { "_id": { "id": "567f18f21e2e328206d3d91e" }, "title": "test", "price": 5

我必须通过
PHP
MongoDB
读取一些数据。 当我将
MongoDB
结果转换为
Json
时,得到如下结果:

{    
    "records":
    [            
        {
            "_id":
            {
                "id": "567f18f21e2e328206d3d91e"
            },
            "title": "test",
            "price": 5000,
            "date": "1394-09-05 11:30",
            "images":
            [
                {
                    "_id":
                    {
                        "id": "567f18f21e2e328206d3d91f"
                    },
                    "url": "a"
                },
                {
                    "_id":
                    {
                        "id": "567f18f21e2e328206d3d920"
                    },
                    "url": "x"
                },
                {
                    "_id":
                    {
                        "id": "567f18f21e2e328206d3d921"
                    },
                    "url": "c"
                }
            ]
        }
    ]
}
"_id":
 {
     "$id": "567f18f21e2e328206d3d91e"
  },
如您所见,我们的id如下所示:

{    
    "records":
    [            
        {
            "_id":
            {
                "id": "567f18f21e2e328206d3d91e"
            },
            "title": "test",
            "price": 5000,
            "date": "1394-09-05 11:30",
            "images":
            [
                {
                    "_id":
                    {
                        "id": "567f18f21e2e328206d3d91f"
                    },
                    "url": "a"
                },
                {
                    "_id":
                    {
                        "id": "567f18f21e2e328206d3d920"
                    },
                    "url": "x"
                },
                {
                    "_id":
                    {
                        "id": "567f18f21e2e328206d3d921"
                    },
                    "url": "c"
                }
            ]
        }
    ]
}
"_id":
 {
     "$id": "567f18f21e2e328206d3d91e"
  },
当我想用Java解析这个json时,它会给我带来一些问题,我想把它转换成这样的东西:

{    
    "records":
    [
        {

            "id": "567f18f21e2e328206d3d91e"           
            "title": "test",
            "price": 5000,
            "date": "1394-09-05 11:30",
            "images":
            [
                {
                    "id": "567f18f21e2e328206d3d91f",
                    "url": "a"
                },
                {
                    "id": "567f18f21e2e328206d3d920",
                    "url": "x"
                },
                {
                    "id": "567f18f21e2e328206d3d921",
                    "url": "c"
                }
            ]
        }
    ]
}
我该怎么做?
我无法使用
foreach
方法编辑此数组,因为我有无限的子数组

首先将json数据转换为数组。从转换后的数组中,您可以格式化所需的图案。我可以帮你

$data = json_decode($old_json_data, true);

$new_data_format = [];
if(isset($data['records'])){
    $data = $data['records'];
    $new_data_format = array_map(function($val){
        return [
            'id' => $val['_id']['id'],
            'title' => $val['title'],
            'price' => $val['price'],
            'date' => $val['date'],
            'images' => array_map(function($v){
                return [
                    'id' => $v['_id']['id'],
                    'url' => $v['url'],
                ];
            }, $val['images'])
        ];
    }, $data);
}

$new_data_format = json_encode(['records' => $new_data_format]);

希望它能帮助您。

非常感谢,但正如我提到的,我在这个数组中有无限子数组,所有子数组都有“\u id”,我应该替换它们。这只是一个示例。这可以用递归函数实现吗?@AliHasanzade,避免使用递归函数的原因是什么?
$data = json_decode($old_json_data, true);

$new_data_format = [];
if(isset($data['records'])){
    $data = $data['records'];
    $new_data_format = array_map(function($val){
        return [
            'id' => $val['_id']['id'],
            'title' => $val['title'],
            'price' => $val['price'],
            'date' => $val['date'],
            'images' => array_map(function($v){
                return [
                    'id' => $v['_id']['id'],
                    'url' => $v['url'],
                ];
            }, $val['images'])
        ];
    }, $data);
}

$new_data_format = json_encode(['records' => $new_data_format]);