如何在PHP中循环使用JSON

时间:2016-07-07 08:02:19

标签: php json foreach

我一直在阅读有关此主题的帖子,但在我的生活中不能用PHP阅读这个JSON字符串。

我想为每个事件获取“summary,descripton,start,end”。

 {
  "pages": {
    "current": 1,
    "total": 1
  },
  "events": [
    {
      "calendar_id": "cal_1",
      "event_uid": "evt_1",
      "summary": "Test",
      "description": "",
      "start": "2016-07-07T04:00:00Z",
      "end": "2016-07-07T05:00:00Z",
      "deleted": false,
      "created": "2016-07-07T04:48:30Z",
      "updated": "2016-07-07T04:51:26Z",
      "actions": {
        "delete": "event_uid"
      },
      "participation_status": "accepted",
      "attendees": [],
      "transparency": "opaque",
      "status": "confirmed",
      "categories": [],
      "recurring": false
    },
    {
      "calendar_id": "cal_1",
      "event_uid": "evt_2",
      "summary": "hyttetur langøyene",
      "description": "",
      "start": "2016-07-08",
      "end": "2016-07-09",
      "deleted": false,
      "created": "2016-07-07T07:11:21Z",
      "updated": "2016-07-07T07:11:21Z",
      "actions": {
        "delete": "event_uid"
      },
      "participation_status": "accepted",
      "attendees": [],
      "transparency": "transparent",
      "status": "confirmed",
      "categories": [],
      "recurring": false
    }
  ]
}

JSON语法看起来不错吗? 任何创建foreach循环的帮助都会非常感激!

var_dump for json_decode:

string(2819) "{"pages":{"current":1,"total":1},"events":[{"calendar_id":"cal_V31TZmjRXTMhAAQD_wckNgeNBT7QOZkRF4PXqqw","event_uid":"evt_external_577d536cd42960ce5b87c9bf","summary":"Ullevål","description":"","start":"2016-07-04T08:30:00Z","end":"2016-07-04T09:30:00Z","deleted":false,"created":"2016-07-04T11:21:41Z","updated":"2016-07-04T11:21:41Z","actions":{"delete":"event_uid"},"location":{"description":"Bygg6 2 etasje"},"participation_status":"accepted","attendees":[],"transparency":"opaque","status":"confirmed","categories":[],"recurring":false},{"calendar_id":"cal_V31TZmjRXTMhAAQD_wckNgeNBT7QOZkRF4PXqqw","event_uid":"evt_external_577ddfced42960ce5b88ad2d","summary":"Test","description":"","start":"2016-07-07T04:00:00Z","end":"2016-07-07T05:00:00Z","deleted":false,"created":"2016-07-07T04:48:30Z","updated":"2016-07-07T04:51:26Z","actions":{"delete":"event_uid"},"participation_status":"accepted","attendees":[],"transparency":"opaque","status":"confirmed","categories":[],"recurring":false},{"calendar_id":"cal_V31TZmjRXTMhAAQD_wckNgeNBT7QOZkRF4PXqqw","event_uid":"evt_external_577de0edd42960ce5b88ad9f","summary":"Ny","description":"","start":"2016-07-07T06:00:00Z","end":"2016-07-07T07:00:00Z","deleted":false,"created":"2016-07-07T04:53:55Z","updated":"2016-07-07T04:56:13Z","actions":{"delete":"event_uid"},"location":{"description":"Sted"},"participation_status":"accepted","attendees":[],"transparency":"opaque","status":"confirmed","categories":[],"recurring":false},{"calendar_id":"cal_V31TZmjRXTMhAAQD_WS4wF1Ceg4RMLThLSHGbRQ","event_uid":"evt_external_577e0098d42960ce5b88c9fe","summary":"siste arbeidsdag!","description":"","start":"2016-07-08","end":"2016-07-09","deleted":false,"created":"2016-07-07T07:11:20Z","updated":"2016-07-07T07:11:20Z","actions":{"delete":"event_uid"},"participation_status":"accepted","attendees":[],"transparency":"transparent","status":"confirmed","categories":[],"recurring":false},{"calendar_id":"cal_V31TZmjRXTMhAAQD_WS4wF1Ceg4RMLThLSHGbRQ","event_uid":"evt_external_577e0099d42960ce5b88c9ff","summary":"hyttetur langøyene","description":"","start":"2016-07-08","end":"2016-07-09","deleted":false,"created":"2016-07-07T07:11:21Z","updated":"2016-07-07T07:11:21Z","actions":{"delete":"event_uid"},"participation_status":"accepted","attendees":[],"transparency":"transparent","status":"confirmed","categories":[],"recurring":false},{"calendar_id":"cal_V31TZmjRXTMhAAQD_WS4wF1Ceg4RMLThLSHGbRQ","event_uid":"evt_external_577e01c1d42960ce5b88ca92","summary":"sykkeltur?","description":"","start":"2016-07-09","end":"2016-07-10","deleted":false,"created":"2016-07-07T07:16:17Z","updated":"2016-07-07T07:16:17Z","actions":{"delete":"event_uid"},"participation_status":"accepted","attendees":[],"transparency":"transparent","status":"confirmed","categories":[],"recurring":false}]}"

print_r for json_decode:

{
pages: {
current: 1,
total: 1
},
events: [
{
calendar_id: "cal_V31TZmjRXTMhAAQD_wckNgeNBT7QOZkRF4PXqqw",
event_uid: "evt_external_577d536cd42960ce5b87c9bf",
summary: "Ullevål",
description: "",
start: "2016-07-04T08:30:00Z",
end: "2016-07-04T09:30:00Z",
deleted: false,
created: "2016-07-04T11:21:41Z",
updated: "2016-07-04T11:21:41Z",
actions: {
delete: "event_uid"
},
location: {
description: "Bygg6 2 etasje"
},
participation_status: "accepted",
attendees: [ ],
transparency: "opaque",
status: "confirmed",
categories: [ ],
recurring: false
},
{
calendar_id: "cal_V31TZmjRXTMhAAQD_wckNgeNBT7QOZkRF4PXqqw",
event_uid: "evt_external_577ddfced42960ce5b88ad2d",
summary: "Test",
description: "",
start: "2016-07-07T04:00:00Z",
end: "2016-07-07T05:00:00Z",
deleted: false,
created: "2016-07-07T04:48:30Z",
updated: "2016-07-07T04:51:26Z",
actions: {
delete: "event_uid"
},
participation_status: "accepted",
attendees: [ ],
transparency: "opaque",
status: "confirmed",
categories: [ ],
recurring: false
},
{
calendar_id: "cal_V31TZmjRXTMhAAQD_wckNgeNBT7QOZkRF4PXqqw",
event_uid: "evt_external_577de0edd42960ce5b88ad9f",
summary: "Ny",
description: "",
start: "2016-07-07T06:00:00Z",
end: "2016-07-07T07:00:00Z",
deleted: false,
created: "2016-07-07T04:53:55Z",
updated: "2016-07-07T04:56:13Z",
actions: {
delete: "event_uid"
},
location: {
description: "Sted"
},
participation_status: "accepted",
attendees: [ ],
transparency: "opaque",
status: "confirmed",
categories: [ ],
recurring: false
},
{
calendar_id: "cal_V31TZmjRXTMhAAQD_WS4wF1Ceg4RMLThLSHGbRQ",
event_uid: "evt_external_577e0098d42960ce5b88c9fe",
summary: "siste arbeidsdag!",
description: "",
start: "2016-07-08",
end: "2016-07-09",
deleted: false,
created: "2016-07-07T07:11:20Z",
updated: "2016-07-07T07:11:20Z",
actions: {
delete: "event_uid"
},
participation_status: "accepted",
attendees: [ ],
transparency: "transparent",
status: "confirmed",
categories: [ ],
recurring: false
},
{
calendar_id: "cal_V31TZmjRXTMhAAQD_WS4wF1Ceg4RMLThLSHGbRQ",
event_uid: "evt_external_577e0099d42960ce5b88c9ff",
summary: "hyttetur langøyene",
description: "",
start: "2016-07-08",
end: "2016-07-09",
deleted: false,
created: "2016-07-07T07:11:21Z",
updated: "2016-07-07T07:11:21Z",
actions: {
delete: "event_uid"
},
participation_status: "accepted",
attendees: [ ],
transparency: "transparent",
status: "confirmed",
categories: [ ],
recurring: false
},
{
calendar_id: "cal_V31TZmjRXTMhAAQD_WS4wF1Ceg4RMLThLSHGbRQ",
event_uid: "evt_external_577e01c1d42960ce5b88ca92",
summary: "sykkeltur?",
description: "",
start: "2016-07-09",
end: "2016-07-10",
deleted: false,
created: "2016-07-07T07:16:17Z",
updated: "2016-07-07T07:16:17Z",
actions: {
delete: "event_uid"
},
participation_status: "accepted",
attendees: [ ],
transparency: "transparent",
status: "confirmed",
categories: [ ],
recurring: false
}
]
}

2 个答案:

答案 0 :(得分:3)

首先解码json:

$data = json_decode($yourJsonString);

如果你的json可以正确解码,那么迭代事件和访问属性:

if ($data !== null) {
    foreach ($data->events as $event) {
        $summary = $event->summary;
        $description = $event->description;
        $start = $event->start;
        $end = $event->end;
    }
}

答案 1 :(得分:1)

你需要json_decode它。然后,您可以使用array_reduce之类的内容来提取所需的数据

这是您的数据

$json = <<<'EOD'
{
  "pages": {
    "current": 1,
    "total": 1
  },
  "events": [
    {
      "calendar_id": "cal_1",
      "event_uid": "evt_1",
      "summary": "Test",
      "description": "",
      "start": "2016-07-07T04:00:00Z",
      "end": "2016-07-07T05:00:00Z",
      "deleted": false,
      "created": "2016-07-07T04:48:30Z",
      "updated": "2016-07-07T04:51:26Z",
      "actions": {
        "delete": "event_uid"
      },
      "participation_status": "accepted",
      "attendees": [],
      "transparency": "opaque",
      "status": "confirmed",
      "categories": [],
      "recurring": false
    },
    {
      "calendar_id": "cal_1",
      "event_uid": "evt_2",
      "summary": "hyttetur langøyene",
      "description": "",
      "start": "2016-07-08",
      "end": "2016-07-09",
      "deleted": false,
      "created": "2016-07-07T07:11:21Z",
      "updated": "2016-07-07T07:11:21Z",
      "actions": {
        "delete": "event_uid"
      },
      "participation_status": "accepted",
      "attendees": [],
      "transparency": "transparent",
      "status": "confirmed",
      "categories": [],
      "recurring": false
    }
  ]
}
EOD;

现在运行此操作来解码操纵数据

$data = json_decode($json);

$events = array_reduce($data->events, function($out,$e) {
  return array_merge($out, [[
    'summary' => $e->summary,
    'description' => $e->description,
    'start' => $e->start,
    'end' => $e->end
  ]]);
}, []);

print_r($events);

输出

// Array
// (
//     [0] => Array
//         (
//             [summary] => Test
//             [description] => 
//             [start] => 2016-07-07T04:00:00Z
//             [end] => 2016-07-07T05:00:00Z
//         )
//     [1] => Array
//         (
//             [summary] => hyttetur langøyene
//             [description] => 
//             [start] => 2016-07-08
//             [end] => 2016-07-09
//         )
// )