我不知道为什么这么难,但我有一些解码的JSON,我想循环使用一些数据来构建一个更小的数组。
下面是我的JSON $jsonData
{
"resultsPage": {
"results": {
"event": [
{
"id":11129128,
"type":"Concert",
"uri":"http://www.songkick.com/concerts/11129128-wild-flag-at-fillmore?utm_source=PARTNER_ID&utm_medium=partner",
"displayName":"Wild Flag at The Fillmore (April 18, 2012)",
"start": {
"time":"20:00:00",
"date":"2012-04-18",
"datetime":"2012-04-18T20:00:00-0800"
},
location": {
"city":"Chicago, IL, US",
"lng":-134.903409,
"lat":37.7842398
},
"venue": {
"id":6239,
"displayName":"The Fillmore",
"uri":"http://www.songkick.com/venues/6239-fillmore?utm_source=PARTNER_ID&utm_medium=partner",
"lng":-122.4332937,
"lat":37.7842398,
"metroArea": {
"id":26330,
"uri":"http://www.songkick.com/metro_areas/26330-us-sf-bay-area?utm_source=PARTNER_ID&utm_medium=partner",
"displayName":"SF Bay Area",
"country": { "displayName":"US" },
"state": { "displayName":"CA" }
}
},
"status":"ok",
"popularity":0.012763
},
{
"id":7923094,
"type":"Concert",
"uri":"http://www.songkick.com/concerts/11129128-wild-flag-at-fillmore?utm_source=PARTNER_ID&utm_medium=partner",
"displayName":"Wild Flag at The Fillmore (April 18, 2012)",
"start": {
"time":"20:00:00",
"date":"2012-04-18",
"datetime":"2012-04-18T20:00:00-0800"
},
location": {
"city":"New York, NY, US",
"lng":63.902374,
"lat":49.7842328
},
"venue": {
"id":6239,
"displayName":"The Fillmore",
"uri":"http://www.songkick.com/venues/6239-fillmore?utm_source=PARTNER_ID&utm_medium=partner",
"lng":-122.4332937,
"lat":37.7842398,
"metroArea": {
"id":26330,
"uri":"http://www.songkick.com/metro_areas/26330-us-sf-bay-area?utm_source=PARTNER_ID&utm_medium=partner",
"displayName":"SF Bay Area",
"country": { "displayName":"US" },
"state": { "displayName":"CA" }
}
},
"status":"ok",
"popularity":0.012763
},
{
"id":89763146,
"type":"Concert",
"uri":"http://www.songkick.com/concerts/11129128-wild-flag-at-fillmore?utm_source=PARTNER_ID&utm_medium=partner",
"displayName":"Wild Flag at The Fillmore (April 18, 2012)",
"start": {
"time":"20:00:00",
"date":"2012-04-18",
"datetime":"2012-04-18T20:00:00-0800"
},
location": {
"city":"Miami, FL, US",
"lng":42.1238243,
"lat":50.7289731
},
"venue": {
"id":6239,
"displayName":"The Fillmore",
"uri":"http://www.songkick.com/venues/6239-fillmore?utm_source=PARTNER_ID&utm_medium=partner",
"lng":-122.4332937,
"lat":37.7842398,
"metroArea": {
"id":26330,
"uri":"http://www.songkick.com/metro_areas/26330-us-sf-bay-area?utm_source=PARTNER_ID&utm_medium=partner",
"displayName":"SF Bay Area",
"country": { "displayName":"US" },
"state": { "displayName":"CA" }
}
},
"status":"ok",
"popularity":0.012763
}
]
},
"totalEntries":24,
"perPage":50,
"page":1,
"status":"ok"
}
}下面是我第一次尝试遍历JSON变量并将我想要的数据解析到一个新的数组中。我不确定是否应该使用push,或者是否有更有效的方法将我想要的数据解析为一个新的数组:
$newArray = array();
foreach($jsonData['resultsPage']['results']['event'] as $val){
$newArray .= "{'id' => $val['id'], 'long' => $val['location']['lng'], 'lat' => $val['location']['lat']}"
}发布于 2019-06-26 07:41:56
尝试如下:
$jsondata_array = json_decode($jsonData, true);
$new_required_arr = $jsondata_array['resultsPage']['results']['event'];
foreach($new_required_arr as $key => $val)
{
//Your logic to create new array with required key value pair.
}发布于 2019-06-26 07:32:40
提供的JSON格式是错误的,索引location在开头缺少双引号。
要解析JSON,可以将JSON转换为数组,并通过数组循环应用逻辑或构建新数组,您可以在true参数中使用json_decode。
$arr = json_decode($json, true);
foreach($arr as $k => $v){
//Your logic
}发布于 2019-06-26 07:35:12
循环的问题是,当元素不存在时,就会得到一个错误,因为您试图访问数组中不存在的元素。
foreach($jsonData as $val) {
}然后,您可以使用您的值进行洞察,并且应该使用isset检查它们。或者,在循环周围创建一个if语句,以防止出现这个问题。
下一个问题是将$newArray定义为数组。但是.是连接一个字符串。因此,您应该定义您的初始变量如下:
$newArray = '';最后一件事是,我在代码中看不到它,但是要解析json字符串,必须首先使用json_decode。为了得到一个物体。如果您想要一个数组,就必须将第二个参数设置为true。
$arr = json_decode($myjson, true);https://stackoverflow.com/questions/56767430
复制相似问题