i have the following Array Structure from Facebook Graph API response.
"data": [
{
"actions": [
{
"action_type": "comment",
"value": "2"
},
{
"action_type": "offsite_conversion",
"value": "1606"
}
],
"date_start": "2017-04-03",
"date_stop": "2017-05-02"
},
{
"actions": [
{
"action_type": "post",
"value": "2"
},
{
"action_type": "post_reaction",
"value": "33"
},
{
"action_type": "page_engagement",
"value": "816"
},
{
"action_type": "post_engagement",
"value": "807"
},
{
"action_type": "offsite_conversion",
"value": "1523"
}
],
"date_start": "2017-04-03",
"date_stop": "2017-05-02"
},
]
The Number of values is flexible and i want to get the value from “offsite_conversion”. Normally i would do it for example like that:
data['data'][0]['actions']['1']['value']
But in that case this doesn’t work because [‘1’] is variable.
5
Answers
So not completely clear what are you trying to achieve, but in a simple way you can just iterate over your
$data
array:Pretend
$json
holds the data from facebookIf
$data['data'][0]['actions'][LAST VALUE]['value']
is what you’re looking for:Your idea of counting should work then:
Use a loop and test the action type.
Barmar has the best approach if you don’t know where it is, but it’s much easier if you want the last one: