Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP foreach to iterate json data
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>any help with this would be appreciated.</p> <p>the josn looks like this:</p> <pre><code>{ "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" } ] } </code></pre> <p>and the php:</p> <pre><code>&lt;?php $json_url = "js/tiles/jsonfeed.js"; $json = file_get_contents($json_url); $links = json_decode($json, TRUE); ?&gt; &lt;ul&gt; &lt;li&gt; &lt;a href="&lt;?php echo $links['data'][1]['linkUrl'] ?&gt;"&gt;&lt;img scr="&lt;?php echo $links['data'][1]['imgUrl']; ?&gt;" alt="&lt;?php echo $links['data'][1]['alt']; ?&gt;" class="share-icon" /&gt;&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt; </code></pre> <p>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. </p> <p>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.</p> <p>Could someone please help me get this working properly?</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload