I am a newbie.
I have a JSON file that contains names and urls. All i want to do is get randomly selected 11 items from the JSON object and display them in HTML.
I can parse and randomly dump the content of the JSON but i don't know what to do more. I am thinking to use foreach for printing html but i don't know how to get the items from JSON object.
Here is the project in my mind;
<div class="custom-top-tags">
<div class="sm-top-tags-title">Öne Çıkanlar:</div>
<div class="sm-top-tags-keywords">
<ul class="sm-top-tags-keywords-list">
<?php foreach($array as $key=>$value){ ?>
<li><a href="<?php echo $key[link]; ?>"><?php echo $key[urun]; ?></a></li>
<?php } ?>
</ul>
</div>
</div>
But unfortunately, i don't know how to fill $key[link]
and $key[name]
variables by randomly selected 11 items from JSON file.
Here is current code that i have;
$json = file_get_contents('./includes/one-cikanlar.json');
$json_data = json_decode($json,true);
shuffle($json_data);
echo $json_data;
Here is the JSON structure;
{
"urunler": [
{
"urun":"Matkap",
"link":"makina-grubu?keyword=Matkap"
},
{
"urun":"İş Eldivenleri",
"link":"eldiven-grubu"
},
{
"urun":"Mikser & Karıştırıcı",
"link":"shop?keyword=Karıştırıcı"
},
{
"urun":"Silikon",
"link":"shop?keyword=Silikon"
},
{
"urun":"Tangitt",
"link":"shop?keyword=Tangit"
},
{
"urun":"Lokma Takımı",
"link":"shop?keyword=Lokma+Takımı"
},
...
]
}
To summarize, i need to get randomly selected 11 items from urunler
object within JSON And print these selected items into HTML.
I am waiting for your suggestions and help. Thanks in advance