
在处理从api或文件中获取的json数据时,我们经常需要根据数据中的某个字段(例如category)对其进行分类、聚合,然后以结构化的方式展示。例如,我们有一个包含文章链接和所属类别的json数组:
[
{
"article": "https://example.com/article1",
"category": "Cat2"
},
{
"article": "https://example.com/article2",
"category": "Cat1"
},
{
"article": "https://example.com/article3",
"category": "Cat1"
},
{
"article": "https://example.com/article4",
"category": "Cat2"
},
{
"article": "https://example.com/article5",
"category": "Cat1"
}
]我们的目标是将其转换为一个PHP数组,其中主键是类别名称,值是该类别下所有文章链接的数组,最终以易于阅读的HTML格式输出:
Cat 1 -- --- first article of cat 1 --- second article of cat 1 --- third article of cat 1 Cat 2 -- --- first article of cat 2 --- second article of cat 2
首先,我们需要将JSON字符串或文件内容解码为PHP可操作的数组。如果JSON数据存储在文件中,可以使用file_get_contents()读取文件内容,然后用json_decode()进行解码。
<?php
// 模拟从文件读取JSON内容
$jsonString = '[{
"article": "https://example.com/article1",
"category": "Cat2"
}, {
"article": "https://example.com/article2",
"category": "Cat1"
}, {
"article": "https://example.com/article3",
"category": "Cat1"
}, {
"article": "https://example.com/article4",
"category": "Cat2"
}, {
"article": "https://example.com/article5",
"category": "Cat1"
}]';
// 将JSON字符串解码为PHP关联数组
// 第二个参数为 true 表示解码为关联数组,而不是对象
$articles = json_decode($jsonString, true);
// 检查解码是否成功
if (json_last_error() !== JSON_ERROR_NONE) {
die("JSON解码失败: " . json_last_error_msg());
}
// 此时 $articles 变量是一个包含所有文章信息的数组
// var_dump($articles);array_column()函数虽然可以提取数组中某一列的值,但它无法直接实现按某个键进行分组聚合。因此,我们需要通过遍历数组的方式手动构建目标结构。
核心思路是:创建一个新的空数组,遍历原始文章数组中的每一个条目。对于每个条目,提取其category值作为新数组的键。如果这个类别键在新数组中尚不存在,则初始化为一个空数组;然后将当前条目的article链接添加到该类别对应的数组中。
立即学习“PHP免费学习笔记(深入)”;
<?php
// ... (接上文的 $articles 变量)
$categorizedArticles = [];
foreach ($articles as $entry) {
$category = $entry['category']; // 获取当前条目的类别
// 如果新数组中尚未存在该类别,则创建一个新的空数组来存储该类别的文章
if (!array_key_exists($category, $categorizedArticles)) {
$categorizedArticles[$category] = [];
}
// 将当前文章的链接添加到对应类别的数组中
$categorizedArticles[$category][] = $entry['article'];
}
// 此时 $categorizedArticles 变量将包含按类别分组后的数据
/*
var_dump($categorizedArticles);
输出示例:
array(2) {
["Cat2"]=>
array(2) {
[0]=> string(24) "https://example.com/article1"
[1]=> string(24) "https://example.com/article4"
}
["Cat1"]=>
array(3) {
[0]=> string(24) "https://example.com/article2"
[1]=> string(24) "https://example.com/article3"
[2]=> string(24) "https://example.com/article5"
}
}
*/数据分组完成后,我们可以使用PHP的循环结构将其渲染成HTML,以实现我们期望的分类展示效果。这通常涉及到嵌套的foreach循环,外层循环遍历类别,内层循环遍历每个类别下的文章。
<?php
// ... (接上文的 $categorizedArticles 变量)
// 使用PHP作为模板引擎直接输出HTML
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>按类别分类的文章</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
h1 { color: #333; border-bottom: 1px solid #eee; padding-bottom: 5px; margin-top: 30px; }
ul { list-style: none; padding-left: 20px; }
li { margin-bottom: 5px; }
a { color: #007bff; text-decoration: none; }
a:hover { text-decoration: underline; }
</style>
</head>
<body>
<?php foreach ($categorizedArticles as $category => $articles): ?>
<h1><?= htmlspecialchars($category); ?></h1>
<ul>
<?php foreach ($articles as $article): ?>
<li><a href="<?= htmlspecialchars($article); ?>" target="_blank"><?= htmlspecialchars($article); ?></a></li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
</body>
</html>上述代码将生成如下HTML输出:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>按类别分类的文章</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
h1 { color: #333; border-bottom: 1px solid #eee; padding-bottom: 5px; margin-top: 30px; }
ul { list-style: none; padding-left: 20px; }
li { margin-bottom: 5px; }
a { color: #007bff; text-decoration: none; }
a:hover { text-decoration: underline; }
</style>
</head>
<body>
<h1>Cat2</h1>
<ul>
<li><a href="https://example.com/article1" target="_blank">https://example.com/article1</a></li>
<li><a href="https://example.com/article4" target="_blank">https://example.com/article4</a></li>
</ul>
<h1>Cat1</h1>
<ul>
<li><a href="https://example.com/article2" target="_blank">https://example.com/article2</a></li>
<li><a href="https://example.com/article3" target="_blank">https://example.com/article3</a></li>
<li><a href="https://example.com/article5" target="_blank">https://example.com/article5</a></li>
</ul>
</body>
</html>通过本教程,我们学习了如何在PHP中有效地处理JSON数据,特别是如何根据特定字段对其进行分类和聚合。这种手动遍历并构建新数组的方法是处理复杂数据结构转换的常用且高效的手段。结合PHP的模板功能,我们可以轻松地将处理后的数据以用户友好的格式展示出来,这对于构建动态网站或数据报告系统至关重要。
以上就是PHP中按类别过滤与展示JSON数据:实用教程的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号