我試圖組查詢結果在PHP作爲以有效的方式關聯數組。 我可以使用多個嵌套的foreach循環,檢查當前的id字段是否與每個數組組的前一個匹配,但我猜測這個問題有更好/更有效的解決方案。 有沒有人有這個聰明的解決方案?理想情況下,一個泛型函數或方法可以將給定關鍵字段或嵌套關鍵字段數組的任何查詢結果集分組?PHP組查詢結果作爲嵌套關聯數組
下面是查詢結果數組:
Array
(
[0] => Array
(
[look_id] => 3
[look_name] => Test Look
[look_description] => description here
[clothing_article_id] => 1
[clothing_article_name] => Coat
[look_clothing_article_attribute_id] => 1
[look_clothing_article_attribute_name] => Purple
[clothing_brand_name] => Gap
[clothing_article_brand_price] => 40.00
[clothing_article_brand_attribute_price] => 50.00
[clothing_article_brand_attribute_name] => Purple
)
[1] => Array
(
[look_id] => 3
[look_name] => Test Look
[look_description] => description here
[clothing_article_id] => 1
[clothing_article_name] => Coat
[look_clothing_article_attribute_id] => 2
[look_clothing_article_attribute_name] => Black
[clothing_brand_name] => Gap
[clothing_article_brand_price] => 40.00
[clothing_article_brand_attribute_price] =>
[clothing_article_brand_attribute_name] =>
)
[2] => Array
(
[look_id] => 3
[look_name] => Test Look
[look_description] => description here
[clothing_article_id] => 2
[clothing_article_name] => Pants
[look_clothing_article_attribute_id] => 3
[look_clothing_article_attribute_name] => Cuffed
[clothing_brand_name] =>
[clothing_article_brand_price] =>
[clothing_article_brand_attribute_price] =>
[clothing_article_brand_attribute_name] =>
)
)
這是我想將其轉換爲數組:
Array
(
'looks' => Array(
[0] => Array(
'id' => 3,
'name' => 'Test Look',
'description' => 'description here',
'clothingArticles' => Array(
[0] => Array(
'id' => 1,
'name' => 'Coat',
'attributes' => Array(
[0] => Array(
'id' => 1,
'name' => 'Purple'
'brands' => Array(
[0] => Array(
'name' => 'Gap',
'price' => 50.00
)
)
),
[1] => Array(
'id' => 2,
'name' => 'Black'
)
),
'brands' => Array(
[0] => Array(
'name' => 'Gap',
'price' => 40.00
)
)
),
[1] => Array(
'id' => 2,
'name' => 'Pants',
'attributes' => Array(
[0] => Array(
'id' => 3,
'name' => 'Cuffed'
'brands' => Array()
)
),
'brands' => Array()
)
)
)
)
)
分組關係的說明:
一看可以有一件或多件服裝用品。 服裝商品可以有一個或多個服裝品牌。 一件外觀的衣服文章可以有一個或多個屬性。 服裝品牌品牌可以有一個或多個屬性。
好吧,到目前爲止試過的是什麼?什麼不工作? – Madbreaks 2013-04-26 21:22:40