Files
Fuxsto-V4/Main/GetGoods/index.php.bak
2025-10-18 10:19:37 +08:00

46 lines
1.3 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
include_once "../Hv4.Function.php";
$语句 = "SELECT * FROM products WHERE status = 'visible';";
$商品元数据 = @数据库运行($语句);
// 原始数据
$data = $商品元数据;
// 用来存储分类结果的数组
$categories = [];
// 分类和整理数据
foreach ($data as $item) {
// 移除分类中的 [xxx] 数字部分
$category1 = preg_replace('/\[\d+\]$/', '', $item['category_1']);
$category2 = preg_replace('/\[\d+\]$/', '', $item['category_2']);
// 分类1和分类2作为键来组织数据
if (!isset($categories[$category1])) {
$categories[$category1] = [];
}
// 确保分类2的存在并添加产品到分类2
if (!isset($categories[$category1][$category2])) {
$categories[$category1][$category2] = [];
}
// 将当前商品添加到相应的分类下
$categories[$category1][$category2][] = $item;
}
// 对每个分类内的数据进行排序
foreach ($categories as $category1 => &$category1Items) {
foreach ($category1Items as $category2 => &$category2Items) {
// 按照 sort_order 字段降序排序
usort($category2Items, function ($a, $b) {
return $b['sort_order'] - $a['sort_order'];
});
}
}
// 转换为 JSON 格式输出
echo 转JSON($categories);
?>