如何高效过滤多个$_GET参数?PHP JSON文件过滤优化
高效实现基于GET参数的JSON文件过滤
问题描述
我通过URL的3个$_GET参数过滤JSON文件,参数示例:keyword=test&date_from=2023-06-01&date_to=2023-06-03,部分参数可能为空(比如keyword=test&date_from=&date_to=2023-06-03)。需要得到过滤后的JSON文件数组$filteredFiles[]。
现有代码能实现日期过滤,但逻辑繁琐,且未完成关键词搜索集成,求高效实现方案。
现有代码:
$customOrders = glob($_SERVER['DOCUMENT_ROOT'].'/data/custombuild/json/*.json'); // array of all custom build json data foreach ($customOrders as $customOrder) { $customOrdersObj[] = json_decode(file_get_contents($customOrder), true); // php assoc array } //iterate over the array // $val['id'] gives me the name of a json file foreach($customOrdersObj as $key => $val) { if(!empty($date_from) && !empty($date_to)) { if($date_val >= $date_from && $date_val <= $date_to) { $filterdFiles[] = $val['id']; // filterd by date_from and date_to } } elseif(!empty($date_from) && empty($date_to)) { // filter by date_from only if($date_val >= $date_from) { $filterdFiles[] = $val['id']; } } elseif(empty($date_from) && !empty($date_to)) { // filter by date_to only if($date_val <= $date_to) { $filterdFiles[] = $val['id']; } } else { // no date filter at all $filterdFiles[] = $val['id']; } // then I also need to integrate the keyword search in it in all possible date combinations: if(in_array($search, $val, true)) { $filterdFiles[] = $val['id']; } }
优化方案
1. 统一处理GET参数,格式化日期
先把参数规整,转成方便比较的格式,避免后续重复判断:
// 提取并初始化过滤参数 $keyword = trim($_GET['keyword'] ?? ''); $dateFrom = !empty($_GET['date_from']) ? $_GET['date_from'] : null; $dateTo = !empty($_GET['date_to']) ? $_GET['date_to'] : null; // 转成时间戳,简化日期范围判断(默认边界设为极值,无参数时自动通过) $timestampFrom = $dateFrom ? strtotime($dateFrom) : -PHP_INT_MAX; $timestampTo = $dateTo ? strtotime($dateTo) : PHP_INT_MAX;
2. 合并过滤逻辑,简化判断流程
把日期和关键词的过滤条件合并,用单一判断替代多层if-else,同时边读文件边过滤,减少内存占用:
$filteredFiles = []; $customOrders = glob($_SERVER['DOCUMENT_ROOT'].'/data/custombuild/json/*.json'); foreach ($customOrders as $filePath) { $orderData = json_decode(file_get_contents($filePath), true); if (!$orderData) continue; // 跳过解析失败的文件 // 替换成你JSON中实际的日期字段,比如$orderData['create_date'] $orderTimestamp = strtotime($orderData['date']); if ($orderTimestamp === false) continue; // 跳过日期格式无效的文件 // 日期过滤:满足时间范围即通过 $datePass = ($orderTimestamp >= $timestampFrom) && ($orderTimestamp <= $timestampTo); // 关键词过滤:无关键词直接通过,有则检查是否存在于数组中 $keywordPass = empty($keyword) || in_array($keyword, $orderData, true); // 两个条件都满足才加入结果 if ($datePass && $keywordPass) { $filteredFiles[] = $orderData['id']; } }
3. 可选增强:模糊关键词搜索
如果需要支持模糊匹配(比如包含关键词而非完全匹配),可以替换in_array为自定义函数:
// 递归遍历数组,检查是否包含关键词(不区分大小写) function hasKeywordMatch(array $data, string $keyword): bool { $lowerKeyword = strtolower($keyword); foreach ($data as $value) { if (is_array($value)) { if (hasKeywordMatch($value, $keyword)) return true; } elseif (is_string($value) && strpos(strtolower($value), $lowerKeyword) !== false) { return true; } } return false; } // 使用时替换关键词判断: $keywordPass = empty($keyword) || hasKeywordMatch($orderData, $keyword);
注意事项
- 代码中的
$orderData['date']需要替换为你JSON文件中实际存储日期的字段名。 - 如果日期格式不是标准的
YYYY-MM-DD,可以用DateTime类来解析,避免strtotime解析失败。
内容的提问来源于stack exchange,提问作者Jack Maessen
相关产品推荐
相关产品推荐

