[英]How to get multi level array value in PHP decoded from JSON
I am facing a problem while printing an array in PHP.我在用 PHP 打印数组时遇到问题。 I am getting data from facebook API for user events and getting the result in JSON.我正在从 facebook API 获取用户事件的数据并以 JSON 格式获取结果。
I just want to get each location (City,Country).我只想得到每个位置(城市,国家)。 Is there any method to get them easily?有什么方法可以轻松获得它们吗?
I have tried:我试过了:
$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 />";
}
When I decode the data and use print_r
:当我解码数据并使用print_r
:
echo "<pre>";
print_r($myjson["data"]);
I get the following result:我得到以下结果:
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
)
)
You have an array of objects, essentially.本质上,您有一组对象。 Since you're force decoding the JSON into an array you have an array of arrays.由于您强制将 JSON 解码为一个数组,因此您有一个数组数组。
Getting the city/country from each object in your example above is pretty simple.从上面示例中的每个对象获取城市/国家非常简单。
foreach($myjson['data'] as $arr) {
echo $arr['place']['location']['city'], ', ', $arr['place']['location']['country'], "\n";
}
The keys mapping to place => location
provide an array with the city
and country
keys you're looking for.映射到place => location
的键提供了一个包含您正在寻找的city
和country
键的数组。 So $myjson['data'][$i][place']['location']['city']
would give you the city and $myjson['data'][$i][place']['location']['country']
would give you the country, but you can iterate over $myjson['data']
with foreach
more conveniently than using a for
loop such as in your example above.所以$myjson['data'][$i][place']['location']['city']
会给你城市和$myjson['data'][$i][place']['location']['country']
会给你国家,但你可以用foreach
迭代$myjson['data']
比使用for
循环更方便,比如上面的例子。
The first element would give you Lahore, Pakistan
, for example.例如,第一个元素会给你Lahore, Pakistan
。
foreach($myjson["data"] as $data){
echo $data['place']['location']['city'];
echo $data['place']['location']['country'];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.