PHP のクエリ結果を効率的な方法で連想配列としてグループ化しようとしています。複数のネストされた foreach ループを使用して、現在の id フィールドが各配列グループの前のフィールドと一致するかどうかを確認できますが、この問題に対するより良い/より効率的な解決策があると思います。誰かがこれに対する賢い解決策を持っていますか? 理想的には、キー フィールドまたはネストされたキー フィールドの配列を指定して、任意のクエリ結果セットをグループ化できる汎用関数またはメソッドですか?
クエリ結果の配列は次のとおりです。
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()
)
)
)
)
)
グループ化関係の説明:
ルックには、1 つまたは複数の衣料品を含めることができます。衣料品には、1 つまたは複数の衣料品ブランドを含めることができます。ルック衣料品は、1 つまたは複数の属性を持つことができます。衣料品ブランドは、1 つまたは複数の属性を持つことができます。