1

我有 JSON 数据,如图所示:

{
    "15": {
        "nid": "15",
        "status": "1",
        "created": "1352652530",
        "changed": "1353780901",
        "title": "some data",
        "type": "some data",
        "tracking_url": {
            "10": {
                "affiliate": "OMG",
                "url_part1": "some data"
            }
        },
        "primary_category": "Travel",
        "active_affiliate": "OMG"
    },
    "16": {
        "nid": "16",
        "status": "1",
        "created": "1352652530",
        "changed": "1353780901",
        "title": "some data",
        "type": "some data",
        "tracking_url": {
            "11": {
                "affiliate": "OMG",
                "url_part1": "some data"
            }
        },
        "primary_category": "Travel",
        "active_affiliate": "OMG"
    }
}

我可以使用以下方法成功解码 JSON:

$response = json_decode($p, true);

现在我必须从(在tracking_url中提取元素)中提取url_part1附属值,这是我无法做到的。$response

4

1 回答 1

1

假设 JSON 结构$p实际上如下所示:

"16": {
    "nid":"16",

    "tracking_url": {
        "10": {
            "affiliate":"OMG",
            "url_part1":"some data"
        }
    }

},

您可以通过以下方式访问 url_part1 和附属机构

$response = json_decode($p, true);

$urlPart = array();
$affiliate = array();

foreach ($response as $nid => $data) {
    $urlPart[$nid]   = $data['tracking_url']['10']['url_part1'];
    $affiliate[$nid] = $data['tracking_url']['10']['affiliate'];
}

这只是如何访问数据的示例,您可以根据需要写入任何需要的变量。

于 2013-03-16T00:12:15.410 回答