PHP foreach 迭代json数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11248019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:05:38  来源:igfitidea点击:

PHP foreach to iterate json data

phpjsonforeach

提问by nate63

I've been working with json for the first time and php for the first time in a long time and I've hit a wall regarding the two.

很长一段时间以来,我第一次使用 json,第一次使用 php,我在这两者上遇到了困难。

I have created a javascript file with a json feed in it and have successfully decoded it using php and can now access the data (one item at a time) but I'm hitting a snag when trying to iterate over it and spit out list items using all of the data.

我已经创建了一个带有 json 提要的 javascript 文件,并使用 php 成功解码了它,现在可以访问数据(一次一个项目),但是当我尝试对其进行迭代并吐出列表项时遇到了障碍使用所有数据。

any help with this would be appreciated.

对此的任何帮助将不胜感激。

the josn looks like this:

josn 看起来像这样:

{
    "data": [
        {
            "name": "name1",
            "alt": "name one",
            "imgUrl": "img/icons/face1.png",
            "linkUrl": "linkurl"
        },
        {
            "name": "name2",
            "alt": "name two",
            "imgUrl": "img/icons/face2.png",
            "linkUrl": "linkurl"
        }
    ]
}

and the php:

和 php:

<?php
$json_url = "js/tiles/jsonfeed.js";
$json = file_get_contents($json_url);
$links = json_decode($json, TRUE);
?>
<ul>
    <li>
    <a href="<?php echo $links['data'][1]['linkUrl'] ?>"><img scr="<?php echo $links['data'][1]['imgUrl']; ?>" alt="<?php echo $links['data'][1]['alt']; ?>" class="share-icon" /></a>
    </li>
</ul>

Now obviously this will only grab the information in the second array slot and I'm aware that there is a more efficient way to write the list items, without jumping in and out of php, and that will be cleaned up shortly.

现在显然这只会获取第二个数组槽中的信息,我知道有一种更有效的方法来编写列表项,而无需跳入和跳出 php,并且很快就会被清除。

The issue I'm having is trying to wrap this in a foreach loop to spit out a list item for each item in the json feed. Being new to json, and having not touched php in some time I'm having a hard time formatting the loop properly and grabbing the appropriate data.

我遇到的问题是试图将其包装在 foreach 循环中,以便为 json 提要中的每个项目吐出一个列表项目。作为 json 的新手,并且有一段时间没有接触过 php,我很难正确格式化循环并获取适当的数据。

Could someone please help me get this working properly?

有人可以帮我让它正常工作吗?

Thanks

谢谢

采纳答案by Havelock

This should do the job:

这应该可以完成这项工作:

<?php
$json_url = "js/tiles/jsonfeed.js";
$json = file_get_contents($json_url);
$links = json_decode($json, TRUE);
?>
<ul>
<?php
    foreach($links['data'] as $key=>$val){ 
?>
    <li>
    <a href="<?php echo $val['linkUrl'] ?>">
        <img scr="<?php echo $val['imgUrl']; ?>" alt="<?php echo $val['alt']; ?>" class="share-icon" />
    </a>
    </li>
<?php
    }
?>
</ul>

回答by ashryalls

You would simply need to foreach the data and then work with each of the children in turn:

您只需要 foreach 数据,然后依次处理每个子项:

foreach ($links['data'] AS $d){
    echo $d['linkUrl'];
}

回答by Brian Glaz

Your foreach loop should iterate over $links['data']like this for example:

您的 foreach 循环应该$links['data']像这样迭代,例如:

foreach($links['data'] as $item) {
    echo $item['linkUrl'];
}