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

PHP合并$help_articles与$related_articles返回指定JSON结构问题咨询

问题原因
  • 你初始化$final为\stdClass对象后,第二行直接执行$final = $help_articles,直接把刚创建的对象覆盖,此时$final的类型和$help_articles一致,是文章列表数组,并非你需要的包含两个字段的外层结构。
  • 你的目标结构需要all_articles字段存储帮助文章数据,但你没有设置这个字段,直接把$help_articles赋值给了$final,结构完全不符合预期。
  • 如果你要以数组语法$final['xxx']赋值,就不能用stdClass类型,对象和数组语法混用会触发PHP报错。
修复方案

直接用数组构造符合要求的结构即可,写法更简洁,适配response()->json()方法的输出要求:

$final = [
    'all_articles' => $help_articles,
    'related_articles' => $related_articles
];
return response()->json($final);

如果你确实需要用stdClass对象实现,就用对象属性语法赋值:

$final = new \stdClass();
$final->all_articles = $help_articles;
$final->related_articles = $related_articles;
return response()->json($final);

两种写法输出的JSON结构都和你预期的完全一致。

内容的提问来源于stack exchange,提问作者Mr. Oleg

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 09:54:02