You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PHP过滤JSON输出,按Start_Date字段统计每月记录数量

解决方案

你已完成JSON到数组的转换,后续只需遍历数据提取月份做统计即可,完整代码如下:

$urls = $ref->multicurlRestApi($urllinkarray, $postfield, $headers);
$decode_open = json_decode($urls[0],true);

// 初始化月份统计数组
$monthCount = [];

// 遍历所有记录
foreach ($decode_open['response']['data'] as $item) {
    // 提取Start_Date的月份部分
    $startDate = $item['fieldData']['Start_Date'];
    $month = (int)explode('/', $startDate)[0];
    // 累加计数
    $monthCount[$month] = ($monthCount[$month] ?? 0) + 1;
}

// 按月份升序排序后输出
ksort($monthCount);
foreach ($monthCount as $month => $num) {
    echo "<p>Month {$month}: {$num}</p>";
}

代码说明:

  • 用explode按/分割日期字符串,第一个元素即为月份,转成int类型避免格式差异
  • 用??运算符简化空值判断,无需额外写isset判断逻辑
  • ksort保证输出时按月份从小到大排序,符合常规展示习惯

内容的提问来源于stack exchange,提问作者Danny Broadhurst

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 18:48:03