Я хочу классифицировать мои данные JSON по идентификатору в разделе «категории». Сейчас я могу вручную изменить то, что я хочу отображать в операторе if, но я хочу, чтобы это сделал фактический пользователь.
Я хочу иметь возможность отображать только данные JSON с категориями-> id = 1/2/3. Можно ли, например, выбрать кнопку «Общие» или «Очистить» сообщения блога с помощью кнопки?
<?php
$json_file = file_get_contents('posts.json', true);
$json = json_decode($json_file);
$posts = $json->posts;
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<title>Blog</title>
</head>
<body>
<?php
foreach ($posts as $post) {
if($post->categories[0]->id == "1"){
?>
<div>
<h2><?php echo $post->title; ?></h2>
<p><?php echo $post->date; ?></p>
<p><?php echo $post->content; ?></p>
<p><?php echo $post->categories[0]->slug; ?></p>
</div>
<?php
}}
?>
</body>
</html>
Вот данные JSON
{
"posts": [
{
"title": "My Lovely Blog",
"date": "22nd September 2018",
"content": "Hey guys so this is my brand new blog that I will be posting on weekly! Stay tuned for more :)",
"categories": [
{
"id": 1,
"slug": "general"
}
]
},
{
"title": "How to clean your home fast: 10 spring cleaning tips",
"date": "29th September 2018",
"content": "Declutter your home: There is a simple rule you can use to declutter you home. If you have not used something...",
"categories": [
{
"id": 2,
"slug": "cleaning"
}
]
},
{
"title": "7 Fashion Trends to Be Thankful for This Year",
"date": "24th November 2018",
"content": "The trickle down effect of this trend might take a while, but on the runways, office-appropriate clothing got a serious makeover. Blazers were paired with embellished short shorts at Prada and loose, 90s-inspired trousers at Tom Ford. At Celine, daywear was rendered in sherbet colors while Michael Kors topped off plunging tops and sarong sequin skirts with wide-cut blazers and flip flops. Just because you have gone corporate does not mean you cannot have fun.",
"categories": [
{
"id":3,
"slug":"fashion"
}
]
}
]
}
Спасибо за любую помощь !!!