PHP循环来自JSON字符串的数据

时间:2015-07-28 10:50:35

标签: php json foreach

我想为每个项目 - > ID

获取 ItemCategory-> id's

我怎样才能以最好的方式做到这一点?

以下是JSON数据项的一部分

"6": {
    "Item": {
      "id": "6",
      "name": "test",
      "description": "description",
    },
    "ItemThumbnail": null,
    "ItemCategory": {
      "3": {
        "id": "3",
        "name": "name",
        "status": "active",
        "date_created": "2015-07-07 11:23:52",
        "date_updated": "0000-00-00 00:00:00",
      },
      "4": {
        "id": "4",
        "name": "name",
        "status": "active",
        "date_created": "2015-07-07 11:23:52",
        "date_updated": "0000-00-00 00:00:00",
      }
    },
    "ItemGroup": []
},

2 个答案:

答案 0 :(得分:1)

您可以使用$array = json_decode($str, true);将json字符串转换为PHP数组,然后循环数组并提取所需的信息。

有关json_decode

的更多详细信息,请参阅http://php.net/manual/en/function.json-decode.php

答案 1 :(得分:1)

在json解码后使用它。

foreach($ Item as $ Items)

{

<system.web>