PHP foreach迭代JSON数据 [英] PHP foreach to iterate json data

查看:134
本文介绍了PHP foreach迭代JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次和json一起工作了很久,第一次使用php,而且我对这两个问题也碰到了一堵墙。



我已经创建了一个json提要的JavaScript文件,并已成功地解码它使用php,现在可以访问数据(一次一个项目),但是当我试图迭代它,并吐出列表项使用所有的数据。

任何帮助,将不胜感激。



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
}
]
}

和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>

现在显然这只会获取第二个数组槽中的信息,一个更有效的方法来编写列表项,而不用跳入和跳出PHP,这将很快被清理。



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

有人可以帮我得到这个工作正常吗?

感谢解决方案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>


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.

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.

any help with this would be appreciated.

the josn looks like this:

{
    "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
$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.

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.

Could someone please help me get this working properly?

Thanks

解决方案

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>

这篇关于PHP foreach迭代JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆