如何使用包含许多组合数据的PHP从JSON中提取数据?

时间:2017-12-30 10:20:08

标签: php json

我有这个包含json的字符串,我想在php中显示数据。 我试过了

$inf = json_decode($myString, true);
echo $inf-> myInfluencers[0]->entity->id;

但没有工作。

{ "myInfluencers":[{"entity":{"id":"123286079528097016","payload":{"kloutId":"123286079528097016",
                                        "nick":"mohammed_mrdi","score":{"score":47.25543551618558,"bucket":"40-49"},"scoreDeltas":{"dayChange":-0.2046305761210192,"weekChange":0.3516640938320563,
                                        "monthChange":0.7581201162824698}}}}
                                        ,{"entity":{"id":"75716803548010325","payload":{"kloutId":"75716803548010325","nick":"araakgroup","score":{"score":54.9323603520146,"bucket":"50-59"}
                                        ,"scoreDeltas":{"dayChange":-0.009862276843456641,"weekChange":0.14323481098688973,"monthChange":8.744154016599673}}}}
                                        ,{"entity":{"id":"165788789554303093","payload":{"kloutId":"165788789554303093","nick":"KypreeAF","score":{"score":68.53700119560989,"bucket":"60-69"},
                                        "scoreDeltas":{"dayChange":-0.04333376347085505,"weekChange":0.3100661156757667,"monthChange":1.8907382052882298}}}}
                                        ,{"entity":{"id":"168040592724049656","payload":{"kloutId":"168040592724049656","nick":"amribrahim98","score":{"score":47.3655074204191,"bucket":"40-49"}
                                        ,"scoreDeltas":{"dayChange":0.007253630369177699,"weekChange":0.361995305845106,"monthChange":-0.12981608352198748}}}}
                                        ,{"entity":{"id":"183521696006191168","payload":{"kloutId":"183521696006191168","nick":"ReemAljeally__","score":{"score":52.861448863866684,"bucket":"50-59"}
                                        ,"scoreDeltas":{"dayChange":-0.09571927285505666,"weekChange":-0.12303670830488045,"monthChange":1.7138913867458498}}}}] }.

2 个答案:

答案 0 :(得分:2)

您需要通过将第二个参数设置为false来将其解码为对象。

$inf = json_decode($myString, false);
echo $inf->myInfluencers[0]->entity->id;

您已将其解码为数组。然后你需要使用它:

$inf = json_decode($myString, true);    
echo $inf['myInfluencers'][0]['entity']['id'];

答案 1 :(得分:0)

$ inf = json_decode($ myString,true);
echo $ inf ['myInfluencers'] [0] ['entity'] ['id'];

这就是语法。谢谢你们