开发者

Looping through JSON Data

开发者 https://www.devze.com 2023-02-17 16:14 出处:网络
There is probably a simple response to this, but I\'ve scoured the net trying to find an answer. Imagine the following JSON dat开发者_开发百科a ($json):

There is probably a simple response to this, but I've scoured the net trying to find an answer.

Imagine the following JSON dat开发者_开发百科a ($json):

{'Top': [{  'Mid1': 'Value1',
            'Mid2': 'Value2',
            'Mid3': [   {'Bottom': 'Value3'},
                        {'Bottom': 'Value4'},
                        {'Bottom': 'Value5'},]
        }]}

What I'd like to do is loop through the data contained within Mid3, so that I can display Bottom1, Bottom2, etc.. so I thought this would work:

foreach($json->Top->Mid3 as $item)
{
echo $item->Bottom;
}

Does anyone know where I'm going wrong?


Your foreach is looking for Mid3 elements and not there child nodes.

Use:

foreach($json->Top->Mid3->Bottom as $item)
{
   echo $item;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号