Reputation: 4055
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.
Below is my 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. 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:
$newArray = array();
foreach($jsonData['resultsPage']['results']['event'] as $val){
$newArray .= "{'id' => $val['id'], 'long' => $val['location']['lng'], 'lat' => $val['location']['lat']}"
}
Upvotes: 0
Views: 178
Reputation: 1642
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.
}
Upvotes: 3
Reputation: 27305
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
. Or you make an if statement around your loop to prevent it that problem.
The next problem is that you define $newArray
as an array. 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. To get an object. If you want an array you have to set the second parameter to true
.
$arr = json_decode($myjson, true);
Upvotes: 1
Reputation: 6388
The provided JSON
format is wrong, the index location
missing the double quote at the beginning.
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.
$arr = json_decode($json, true);
foreach($arr as $k => $v){
//Your logic
}
Upvotes: 1