【发布时间】:2015-10-22 09:39:16
【问题描述】:
在 PHP 中打印数组时遇到问题。我正在从 facebook API 获取用户事件的数据并以 JSON 格式获取结果。
我只想获取每个位置(城市、国家/地区)。有什么方法可以轻松搞定?
我试过了:
$myjson=json_decode($_POST['Data'],true);
echo "<pre>";
print_r($myjson["data"]);
echo "<pre>";
echo count($myjson["data"]);
echo "<pre>";
for($i =0; $i<count($myjson["data"]); $i++){
print($myjson["data"][$i]["name"]);
echo "<br />";
}
当我解码数据并使用print_r:
echo "<pre>";
print_r($myjson["data"]);
我得到以下结果:
Array (
[0] => Array
(
[description] => this is test
[name] => test event Web Developer
[place] => Array
(
[name] => Lahore
[location] => Array
(
[city] => Lahore
[country] => Pakistan
[latitude] => 31.5497
[longitude] => 74.3436
)
[id] => 108104849224069
)
[start_time] => 2015-10-21T19:00:00+0500
[id] => 1503962379902466
[rsvp_status] => attending
)
[1] => Array
(
[description] => (Full description omitted for brevity)
[name] => Peaceful Pakistan Online Competition!
[place] => Array
(
[name] => Punjab Information Technology Board (PITB)
[location] => Array
(
[city] => Lahore
[country] => Pakistan
[latitude] => 31.5497
[longitude] => 74.3436
[zip] => 54600
)
[id] => 340860042630070
)
[start_time] => 2015-08-10T00:00:00+0500
[id] => 907352572664461
[rsvp_status] => attending
)
[2] => Array
(
[description] => (Full description omitted for brevity)
[end_time] => 2015-06-17T19:30:00+0500
[name] => Open House 2015
[place] => Array
(
[name] => 6th Floor, Arfa Software Technology Park, 346-B, Ferozepur Road, Lahore, Pakistan-54000
)
[start_time] => 2015-06-17T16:30:00+0500
[id] => 1619010958352625
[rsvp_status] => attending
)
[3] => Array
(
[description] => A Two Day Workshop on Game Design and Development on Unity 3d
[end_time] => 2015-04-26T16:00:00+0500
[name] => Game Design And Development Workshop
[place] => Array
(
[name] => House #75, Sarfaraz Rafique Road, Opposite P.A.F Sports Stadium, Lahore Cantt
)
[start_time] => 2015-04-26T10:00:00+0500
[id] => 1651929135026796
[rsvp_status] => attending
)
[4] => Array
(
[description] => (Full description omitted for brevity)
[name] => Live Chat with Experts: How Does Pakistan's Load Shedding Affect You?
[place] => Array
(
[name] => Islamabad, Pakistan
)
[start_time] => 2015-03-26T11:00:00-0400
[id] => 347677685418059
[rsvp_status] => attending
)
[5] => Array
(
[description] => (Full description omitted for brevity)
[name] => 6th September – Defense Day of Pakistan
[place] => Array
(
[name] => Virtual Event
)
[start_time] => 2013-09-06
[id] => 434438063339987
[rsvp_status] => attending
)
)
【问题讨论】: