dongmi5177 2016-09-26 11:51
浏览 120
已采纳

如何在PHP中循环JSON对象值?

I have a JSON Object and I want to loop through the values:

$json = '{"1":a,"2":b,"3":c,"4":d,"5":e}';
$obj = json_decode($json, TRUE);
for($i=0; $i<count($obj['a']); $i++) {
    echo $i;
}

I want the $i to display the abcde that are the values in the object.

  • 写回答

2条回答 默认 最新

  • douzhi1972 2016-09-26 11:55
    关注

    Try using.

    $json = '{"1":"a","2":"b","3":"c","4":"d","5":"e"}';
     $obj = json_decode($json, TRUE);
    
    foreach($obj as $key => $value) 
    {
    echo 'Your key is: '.$key.' and the value of the key is:'.$value;
    }
    

    p.s not tested ;-)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?