I’ve searched and read through some of the responses and similar questions, but still can’t get my php foreach loop to parse and display the list of items in html.
cats.json file:
{
"cats": [
{
"id": "1",
"type": "main coon",
"sex": "male",
"name": "chocolate"
},
{
"id": "2",
"type": "tabby",
"sex": "female",
"name": "butters"
},
{
"id": "3",
"type": "alley",
"sex": "male",
"name": "oscar"
}
]
}
HTML w/ PHP
<?php
$cats_file = ('./_inc/cats.json');
$cats_data = file_get_contents($cats_file);
$cats_json = json_decode($cats_data, TRUE);
foreach ($cats_json AS $cat):
?>
<div>Some HTML</div>
<h3><strong><?php echo $cat['name'];?></strong></h3>
<div>some more HTML</div>
<?php endforeach;?>
When I run the loop I get the following error message:
WARNING: UNDEFINED ARRAY KEY “name” IN E:HTDOCS…”
It seems straight forward, but the solution is eluding me and can’t seem to figure out why it’s not working even after trying various similar posts.
What I would like it to do is display a list of each cat and that’s it. Nothing fancy.