我在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
)
)
答案 0 :(得分:1)
基本上你有一个对象数组。由于您强制将JSON解码为数组,因此您有一个数组数组。
从上面示例中的每个对象获取城市/国家非常简单。
foreach($myjson['data'] as $arr) {
echo $arr['place']['location']['city'], ', ', $arr['place']['location']['country'], "\n";
}
映射到place => location
的键提供了一个包含您正在寻找的city
和country
键的数组。因此,$myjson['data'][$i][place']['location']['city']
会为您提供该城市,$myjson['data'][$i][place']['location']['country']
会为您提供该国家/地区,但您可以使用$myjson['data']
比使用foreach
循环更方便地遍历for
例如在上面的例子中。
第一个元素会给你Lahore, Pakistan
,例如。
答案 1 :(得分:0)
foreach($myjson["data"] as $data){
echo $data['place']['location']['city'];
echo $data['place']['location']['country'];
}