我在嘗試構(gòu)建一個(gè)數(shù)組時(shí)遇到問(wèn)題,其中狀態(tài) ID 是鍵,并且與狀態(tài)相關(guān)的所有帖子都是與鍵(狀態(tài) ID)相關(guān)的子數(shù)組。這是我通過(guò) array_merge_recursive 和手動(dòng)將項(xiàng)目添加到數(shù)組(數(shù)組 1)得到的(不正確的)數(shù)組: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 ) )這是我嘗試過(guò)的:運(yùn)行兩個(gè)單獨(dú)的數(shù)據(jù)庫(kù)查詢(xún):一個(gè)用于獲取狀態(tài),另一個(gè)用于獲取帖子,然后遞歸合并數(shù)組并將數(shù)組鍵更改為字符串。這做了同樣的事情,post 5 永遠(yuǎn)不會(huì)出現(xiàn)在新合并的數(shù)組中。與上面相同 - 運(yùn)行兩個(gè)單獨(dú)的查詢(xún)并手動(dòng)重建數(shù)組,第五篇文章永遠(yuǎn)不會(huì)出現(xiàn)相同的結(jié)果。我直接從數(shù)據(jù)庫(kù)打印出了結(jié)果$stmt2->fetchAll();集中所有 5 個(gè)帖子的數(shù)據(jù)庫(kù)結(jié)果。在合并數(shù)組或構(gòu)建新數(shù)組時(shí),第五個(gè)不會(huì)保留,以便帖子可以與狀態(tài)相關(guān)。我也嘗試使用 SQL 連接表,但即使按 resolution_id 分組也會(huì)做同樣的事情,第 5 號(hào)帖子會(huì)因分組而丟失。我也嘗試過(guò)子查詢(xún)。
1 回答

飲歌長(zhǎng)嘯
TA貢獻(xiàn)1951條經(jīng)驗(yàn) 獲得超3個(gè)贊
要構(gòu)建帖子數(shù)組,您需要將元素附加到帖子數(shù)組中。目前,您只是一遍又一遍地將單個(gè)元素分配給數(shù)組,這會(huì)覆蓋整個(gè)數(shù)組的先前值。
附加帖子的代碼:
$statuses['res_' . $row2['resolution_id']]['post'][] = ['post_title' => $row2['title'], 'post_description' => $row2['description'], 'categories' => $row2['category_names']];
請(qǐng)注意[]
我添加到賦值運(yùn)算符左側(cè)末尾的 。
- 1 回答
- 0 關(guān)注
- 141 瀏覽
添加回答
舉報(bào)
0/150
提交
取消