我在嘗試構建一個數組時遇到問題,其中狀態 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 關注
- 132 瀏覽
添加回答
舉報
0/150
提交
取消