我有php API(使用PDO)。我從客戶端獲取一些數(shù)據(jù),并對其應用mysql查詢,并從數(shù)據(jù)庫接收查詢結(jié)果。現(xiàn)在,我想要的是將查詢結(jié)果拆分為多個數(shù)組(多維數(shù)組)。就像Im將購物車的Item(11,12,13,17,17,18,19)ID發(fā)送到數(shù)據(jù)庫查詢一樣,如下所示:"SELECT s.SparePartID, s.Name, s.VendorID, v.CompanyName FROM sparepart s INNER JOIN users vON v.VendorID= s.VendorIDWHERE s.SparePartID IN ('11', '12','13', '17', '18', '19')"以上查詢結(jié)果如下:+-------------+---------+----------+---------------------+| SparePartID | Name | VendorID | CompanyName |+=============+=========+==========+=====================+| 11 | Tyres | 48 | Master Automotives |+-------------+---------+----------+---------------------+| 12 | Lights | 48 | Master Automotives |+-------------+---------+----------+---------------------+| 13 | Wheels | 48 | Master Automotives |+-------------+---------+----------+---------------------+| 17 | Torch | 50 | *Repair Solutions* |+-------------+---------+----------+---------------------+| 18 | Lamp | 50 | *Repair Solutions* |+-------------+---------+----------+---------------------+ | 19 | Camera | 50 | *Repair Solutions* |+-------------+---------+----------+---------------------+現(xiàn)在,我想它的保存群體的陣列其公司名稱的基礎(chǔ)上。因為此數(shù)據(jù)中有2個不同的公司名稱,所以我想將此數(shù)據(jù)保存在2個數(shù)組的數(shù)組中,否則組數(shù)應等于不同的公司名稱。因此,我希望將蕩婦分成幾個部分:["Group1": [{"SparePartID": "11","Name" : "Tyres","VendorID": "48","Company" : "Master Automotives"},{"SparePartID": "12","Name" : "Lights","VendorID": "48","Company" : "Master Automotives"},{"SparePartID": "13","Name" : "Wheels","VendorID": "50","Company" : "Master Automotives"}]"Group2": [{"SparePartID": "17","Name" : "Torch","VendorID": "50","Company" : "*Repair Solutions*"},{"SparePartID": "18","Name" : "Lamp","VendorID": "50","Company" : "*Repair Solutions*"},{"SparePartID": "19","Name" : "Camera","VendorID": "50","Company" : "*Repair Solutions*"}]]現(xiàn)在,請任何人幫助我建議如何根據(jù)公司名稱拆分結(jié)果?執(zhí)行查詢后,我應該修改查詢還是修改結(jié)果數(shù)組嗎?請幫助
2 回答

MM們
TA貢獻1886條經(jīng)驗 獲得超2個贊
只需更改此行:
array_push($products_arr["records"], $product_item);
對此:
$products_arr["records"][$product_item["Company"]][] = $product_item;

呼喚遠方
TA貢獻1856條經(jīng)驗 獲得超11個贊
試試這個查詢:
SELECT s.SparePartID, s.Name, s.VendorID, v.CompanyName FROM sparepart s
INNER JOIN users v ON v.VendorID= s.VendorID WHERE s.SparePartID IN
({$ids_arr}) **GROUP BY v.CompanyName**
- 2 回答
- 0 關(guān)注
- 267 瀏覽
添加回答
舉報
0/150
提交
取消