[英]PHP Loop through JSON to create a new array
I am not sure why I am having such a hard time with this but I have some Decoded JSON and I want to loop through it to build a smaller array using some of the data. 我不确定为什么会遇到这么麻烦,但是我有一些解码后的JSON,我想遍历它来使用一些数据构建一个较小的数组。
Below is my JSON $jsonData
: 以下是我的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"
}
}
Below is my first try at looping through the JSON variable and parsing out the data I want into a new array. 下面是我的第一次尝试通过JSON变量循环和我想要的数据解析出到一个新的数组。 I am not sure if I should use push or if there is a more effiecent way to parse the data I want into a new array:
我不知道我是否应该使用推或有分析数据更effiecent办法,我想成为一个新的数组:
$newArray = array();
foreach($jsonData['resultsPage']['results']['event'] as $val){
$newArray .= "{'id' => $val['id'], 'long' => $val['location']['lng'], 'lat' => $val['location']['lat']}"
}
Try this: 尝试这个:
$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.
}
The provided JSON
format is wrong, the index location
missing the double quote at the beginning. 提供的
JSON
格式错误,索引location
开头缺少双引号。
To parse the JSON
, you can convert JSON
to array and loop through the array to apply your logic OR build new array, you can use json_decode
with true
parameter. 要解析
JSON
,您可以将JSON
转换为数组并遍历数组以应用逻辑或构建新数组,可以将json_decode
与true
参数一起使用。
$arr = json_decode($json, true);
foreach($arr as $k => $v){
//Your logic
}
The problem with you loop is when the element don't exist you get an error because you try to access an element in your array which doesn't exist. 循环的问题是,当元素不存在时,您会收到错误消息,因为您尝试访问数组中不存在的元素。
foreach($jsonData as $val) {
}
Then you can work insight with your values and you should check them with isset
. 然后,您可以使用您的值进行洞察,应该使用
isset
对其进行检查。 Or you make an if statement around your loop to prevent it that problem. 或者,您可以在循环周围执行if语句来防止该问题的发生。
The next problem is that you define $newArray
as an array. 下一个问题是您将
$newArray
定义为一个数组。 But the .
但是
.
is to concat a string. 是连接一个字符串。 So you should define your initial variable like this:
因此,您应该像这样定义初始变量:
$newArray = '';
The last thing is i can't see it in your code but to parse a json string you have to use json_decode
first. 最后一件事是我在您的代码中看不到它,但要解析json字符串,您必须先使用
json_decode
。 To get an object. 得到一个对象。 If you want an array you have to set the second parameter to
true
. 如果需要数组,则必须将第二个参数设置为
true
。
$arr = json_decode($myjson, true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.