简体   繁体   English

如何在PHP中访问多层JSON树以进行数据库插入

[英]How to access multi-level json tree in PHP for database insertion

I am accessing an API where the JSON data is returned as below. 我正在访问一个API,其中返回JSON数据的方法如下。

{
"name": "",
"count": 4,
"frequency": "",
"version": 3,
"newdata": true,
"lastrunstatus": "success",
"thisversionstatus": "success",
"thisversionrun": "",
"results": {
    "collection": [{
        "textlink": {
            "href": "http://text1.com",
            "text": "Text 1"
        },
        "index": 1,
        "url": ""
    }, {
        "textlink": {
            "href": "http://text2.com",
            "text": "Text 2"
        },
        "index": 2,
        "url": ""
    }, {
        "textlink": {
            "href": "http://text3.com",
            "text": "Text 3"
        },
        "index": 3,
        "url": ""
    }, {
        "textlink": {
            "href": "http://text4.com",
            "text": "Text 4"
        },
        "index": 4,
        "url": ""
    }]
}}

As you can see the JSON tree returned has multi-step levels. 如您所见,返回的JSON树具有多步骤级别。 I am wanting to be able to take some of the deeper levels, in PHP, and insert into a database. 我希望能够采用PHP的一些更深层次的知识,并将其插入数据库中。

Currently I am using this code to try and echo the data (once I am able to I can then work on inserting it to a database no problem) 目前,我正在使用此代码尝试回显数据(一旦我能够将其插入数据库就可以了)

<?php
$request = "API.REQUEST.NET";
$response = file_get_contents($request);
$results = json_decode($response, TRUE);

foreach($results as $item) {

echo $item->results[0]->collection[0]->textlink[0]->href;
echo "<br>";
echo $item->results->collection['href'];
echo "<br>";
echo $item->results->collection['text'];

}
?>

As you can see above I have tried several ways to access the deeper levels f data that are being displayed but with no avail. 如您在上面看到的,我尝试了几种方法来访问正在显示但无济于事的更深层次的f数据。

I am currently getting errors of 'trying to get property of a non-object'. 我目前遇到“尝试获取非对象属性”的错误。 How is it possible to reach the data in this array? 如何到达此数组中的数据?

尝试:

echo $results['results']['collection'][0]['textlink']['href'];

I would suggest to do something like this (According to me, correct way to fetch results from API responses): 我建议做这样的事情(据我说,从API响应中获取结果的正确方法):

<?php
$request = "API.REQUEST.NET";
$response = file_get_contents($request);

$response = json_decode($response);

foreach($response->results->collection as $item) {

echo $item->textlink->href;
echo "<br>";
echo $item->textlink->text;
echo "<br>";
echo $item->index;
echo "<br>";
echo $item->url;

}
?>
$obj = json_decode( $json, true ); foreach ( $obj['key'] as $key => $value ) { echo $value; }
foreach ($response['results']['collection'] as $textlink) {
 $row = $textlink['textlink'];
 echo $row['href'];
 echo "<br>";
 echo $row['text'];
 echo "<br>";
}

you can do foreach like this, which loops only items in collection 您可以像这样做foreach,它仅循环收集中的项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM