我在尝试构建一个数组时遇到问题,其中状态 ID 是键,并且与状态相关的所有帖子都是与键(状态 ID)相关的子数组。这是我通过 array_merge_recursive 和手动将项目添加到数组(数组 1)得到的(不正确的)数组:Array( [res_1] => Array ( [status_name] => NEEDS REVIEW [status_color] => 666666 [post] => Array ( [post_title] => Add feature that allows our team to add internal notes on ideas [post_description] => Sometimes our team needs to leave feedback that users should not see publicly. Having this feature would allow the team to collaborate better at scale. [categories] => Admin,Communication ) ) [res_2] => Array ( [status_name] => PLANNED [status_color] => aa5c9e [post] => Array ( [post_title] => Add support for multiple languages [post_description] => We have customers across the globe who would appreciate this page to be localized to their language [categories] => Integrations ) ) [res_3] => Array ( [status_name] => IN PROGRESS [status_color] => 3333cc [post] => Array ( [post_title] => Allow users to add an image with their feature request [post_description] => Sometimes users want something visual, having an example really helps. [categories] => Uncategorized ) )这是我尝试过的:运行两个单独的数据库查询:一个用于获取状态,另一个用于获取帖子,然后递归合并数组并将数组键更改为字符串。这做了同样的事情,post 5 永远不会出现在新合并的数组中。与上面相同 - 运行两个单独的查询并手动重建数组,第五篇文章永远不会出现相同的结果。我直接从数据库打印出了结果$stmt2->fetchAll();集中所有 5 个帖子的数据库结果。在合并数组或构建新数组时,第五个不会保留,以便帖子可以与状态相关。我也尝试使用 SQL 连接表,但即使按 resolution_id 分组也会做同样的事情,第 5 号帖子会因分组而丢失。我也尝试过子查询。
1 回答
饮歌长啸
TA贡献1951条经验 获得超3个赞
要构建帖子数组,您需要将元素附加到帖子数组中。目前,您只是一遍又一遍地将单个元素分配给数组,这会覆盖整个数组的先前值。
附加帖子的代码:
$statuses['res_' . $row2['resolution_id']]['post'][] = ['post_title' => $row2['title'], 'post_description' => $row2['description'], 'categories' => $row2['category_names']];
请注意[]
我添加到赋值运算符左侧末尾的 。
- 1 回答
- 0 关注
- 79 浏览
添加回答
举报
0/150
提交
取消