PHP:从没有循环JSON数组获取数据 [英] PHP: get data from json array without loop
本文介绍了PHP:从没有循环JSON数组获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的数据我想从数组以得到无循环。
我想要得到的link_click,价值例如,我怎样才能使这项工作?
我想:但是这不工作
。 $统计等于json_de code($的数据,FALSE);
$ link_click = $ stats->数据 - >动作 - > action_type-> ['link_click'] - >价值;
{
数据:
{
行动:[
{
ACTION_TYPE:评论,
值:2
},
{
ACTION_TYPE:link_click,
值:636
},
{
ACTION_TYPE:post_like,
值:2
},
{
ACTION_TYPE:page_engagement,
值:640
},
{
ACTION_TYPE:post_engagement,
值:640
}
]
解决方案
可以使人们有可能只有当你知道 ACTION_TYPE指数的唯一方法:link_click
。如果你知道索引,可以做到这一点。 (答案是相对于你上面显示的数据)。
$统计等于json_de code($的数据,真实);
$ link_click = $统计['数据'] ['行动'] [1] ['值'];
循环示例(收费):
$统计等于json_de code($的数据,真实);
$值= 0;
的foreach($统计['数据'] ['行动']为$行动){
如果($行动 - > ACTION_TYPE =='link_click'){
$值= $行动 - >价值;
}
}回声$价值; //这是你的价值
I have data i want to get from array without loop.
I want to get "value" of "link_click" for example, how can i make this work?
I tried: but this not working.
$stats = json_decode($data, false);
$link_click= $stats->data->actions->action_type->['link_click']->value;
{
"data": [
{
"actions": [
{
"action_type": "comment",
"value": 2
},
{
"action_type": "link_click",
"value": 636
},
{
"action_type": "post_like",
"value": 2
},
{
"action_type": "page_engagement",
"value": 640
},
{
"action_type": "post_engagement",
"value": 640
}
],
解决方案
The only way you can make it possible only if you know the index of the action_type:link_click
. If you know the index, you can do it by. (Answer is with respect to the data you have shown above).
$stats = json_decode($data, true);
$link_click= $stats['data']['actions'][1]['value'];
Loop example (on request):
$stats = json_decode($data, true);
$value = 0;
foreach($stats['data']['actions'] as $action) {
if ($action->action_type == 'link_click') {
$value = $action->value;
}
}
echo $value; //This is your value
这篇关于PHP:从没有循环JSON数组获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文