
在现代web开发中,json(javascript object notation)已成为一种主流的数据交换格式,广泛应用于前后端数据传输、api接口响应等场景。php作为后端编程语言,经常需要处理json数据。本教程将指导您如何使用php解析json字符串,并根据其中的某个字段(例如“类别”)对数据进行分组,最终以结构化的html形式展示出来。
假设我们有一个包含多篇文章信息的JSON数组,每篇文章都有“文章链接”、“类别”和“标题”三个字段。其基本结构如下:
[
{
"article": "https://example.com/article1",
"category": "Cat2",
"title": "1the title Cat2"
},
{
"article": "https://example.com/article2",
"category": "Cat1",
"title": "1the title Cat1"
},
// ... 更多文章
]我们的目标是将这些扁平化的文章列表,按照category字段进行分组,然后依次显示每个类别下的所有文章信息。
PHP提供了内置函数json_decode()来解析JSON字符串。这个函数可以将JSON字符串转换为PHP变量。
<?php
$json = '[{
"article": "https://example.com",
"category": "Cat2",
"title" : "1the title Cat2"
}, {
"article": "https://example.com",
"category": "Cat1",
"title" : "1the title Cat1"
}, {
"article": "https://example.com",
"category": "Cat1",
"title" : "2the title Cat1"
}, {
"article": "https://example.com",
"category": "Cat2",
"title" : "2the title Cat2"
}, {
"article": "https://example.com",
"category": "Cat1",
"title" : "3the title Cat1"
}]';
// 使用 json_decode() 将JSON字符串解析为PHP数组
// 第二个参数设置为 true,表示将JSON对象解析为PHP关联数组
$values = json_decode($json, true);
// 此时 $values 将是一个包含多个关联数组的数组
// print_r($values); // 可以取消注释查看解析结果
?>json_decode()的第二个参数设置为true至关重要,它确保JSON对象被解析为PHP关联数组,而不是PHP对象,这使得通过键名访问数据更加直观。
立即学习“PHP免费学习笔记(深入)”;
原始数据是一个简单的文章列表,为了按类别展示,我们需要将其重构为一个新的数组结构:以类别名称为键,值为该类别下所有文章组成的数组。
<?php
// ... (接上文的 $json 和 $values 定义)
$res = []; // 初始化一个空数组,用于存储按类别分组后的数据
// 遍历解析后的文章数组
foreach ($values as $entry) {
$category = $entry['category']; // 获取当前文章的类别
// 检查 $res 数组中是否已存在该类别作为键
if (! array_key_exists($category, $res)) {
// 如果不存在,则创建该类别键,并初始化为一个空数组
$res[$category] = [];
}
// 将当前文章($entry)添加到对应类别的数组中
$res[$category][] = $entry;
}
// 此时 $res 数组的结构将是:
// [
// "Cat2" => [
// ["article" => "...", "category" => "Cat2", "title" => "..."],
// ["article" => "...", "category" => "Cat2", "title" => "..."]
// ],
// "Cat1" => [
// ["article" => "...", "category" => "Cat1", "title" => "..."],
// // ...
// ]
// ]
// print_r($res); // 可以取消注释查看分组结果
?>这段代码通过一个循环遍历所有文章。对于每篇文章,它提取其类别,然后检查我们用于存储结果的$res数组中是否已经有这个类别作为键。如果没有,就创建一个新的键并将其值初始化为一个空数组。最后,将当前文章的完整信息添加到对应类别的数组中。
数据重组完成后,我们可以遍历新的$res数组,生成结构化的HTML输出。外层循环用于处理每个类别,内层循环则处理每个类别下的所有文章。
<html>
<head>
<title>文章分类展示</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
h1 { color: #333; border-bottom: 2px solid #eee; padding-bottom: 5px; margin-top: 30px; }
p { margin: 5px 0; }
a { color: #007bff; text-decoration: none; }
a:hover { text-decoration: underline; }
</style>
</head>
<body>
<?php
// ... (接上文的 $json, $values, $res 定义)
// 遍历按类别分组后的数据
foreach($res as $category => $articles_in_category): ?>
<h1><?= htmlspecialchars($category); ?></h1> <!-- 输出类别标题 -->
<?php
// 遍历当前类别下的所有文章
foreach($articles_in_category as $article): ?>
<p>链接: <a href="<?= htmlspecialchars($article['article']); ?>"><?= htmlspecialchars($article['article']); ?></a></p>
<p>标题: <?= htmlspecialchars($article['title']); ?></p>
<?php endforeach; ?>
<?php endforeach;
?>
</body>
</html>关键修正点: 在原始的问题代码中,内层循环尝试通过$entry['title']访问标题,但$entry在内层循环中代表的是整个类别下的文章数组,而不是单个文章。正确的做法是使用内层循环变量$article来访问单个文章的字段,即$article['article']和$article['title']。
此外,为了防止跨站脚本(XSS)攻击,我们对所有动态输出的内容都使用了htmlspecialchars()函数进行转义,这是一个良好的安全实践。
将上述所有代码片段整合,构成一个完整的PHP文件,可以直接运行以查看效果。
<html>
<head>
<title>文章分类展示</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
h1 { color: #333; border-bottom: 2px solid #eee; padding-bottom: 5px; margin-top: 30px; }
p { margin: 5px 0; }
a { color: #007bff; text-decoration: none; }
a:hover { text-decoration: underline; }
</style>
</head>
<body>
<?php
$json = '[{
"article": "https://example.com",
"category": "Cat2",
"title" : "1the title Cat2"
}, {
"article": "https://example.com",
"category": "Cat1",
"title" : "1the title Cat1"
}, {
"article": "https://example.com",
"category": "Cat1",
"title" : "2the title Cat1"
}, {
"article": "https://example.com",
"category": "Cat2",
"title" : "2the title Cat2"
}, {
"article": "https://example.com",
"category": "Cat1",
"title" : "3the title Cat1"
}]';
$values = json_decode($json, true);
$res = [];
foreach ($values as $entry) {
$category = $entry['category'];
if (! array_key_exists($category, $res)) {
$res[$category] = [];
}
$res[$category][] = $entry;
}
foreach($res as $category => $articles_in_category): ?>
<h1><?= htmlspecialchars($category); ?></h1>
<?php foreach($articles_in_category as $article): ?>
<p>链接: <a href="<?= htmlspecialchars($article['article']); ?>"><?= htmlspecialchars($article['article']); ?></a></p>
<p>标题: <?= htmlspecialchars($article['title']); ?></p>
<?php endforeach; ?>
<?php endforeach;
?>
</body>
</html>$values = json_decode($json, true);
if (json_last_error() !== JSON_ERROR_NONE) {
echo "JSON解析错误: " . json_last_error_msg();
// 终止脚本或进行其他错误处理
exit;
}通过本教程,您学会了如何利用PHP的json_decode()函数解析JSON数据,并通过循环和条件判断将数据按特定字段进行高效分组。最后,我们演示了如何将重组后的数据以结构化且安全的HTML格式输出。掌握这些技巧将使您能够更灵活、更专业地处理和展示Web应用中的JSON数据。
以上就是PHP JSON数据解析与按类别分组展示教程的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号