2013-04-26 108 views
1

我試圖組查詢結果在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() 
       ) 
      ) 
     ) 
    ) 
) 

分組關係的說明:

一看可以有一件或多件服裝用品。 服裝商品可以有一個或多個服裝品牌。 一件外觀的衣服文章可以有一個或多個屬性。 服裝品牌品牌可以有一個或多個屬性。

+2

好吧,到目前爲止試過的是什麼?什麼不工作? – Madbreaks 2013-04-26 21:22:40

回答

1

這不應該是太嚇人做;而不是使用內部數組的數字索引,您應該使用任何唯一標識符,以便您可以對其進行分組。這並不妨礙您在數組本身中使用該標識符。

//seems a little pointless? 
$looks = array('looks' => array()); 
$looks = &$looks['looks']; 
while ($row = $result->fetch()) { 
    if (!isset($looks[$row->look_id])) { 
     $looks[$row->look_id] = array(
      'id' => $row->look_id, 
      'name' => $row->look_name, 
      'description' => $row->look_description, 
      'clothingArticles' => array() 
     ); 
    } 
    $look = &$looks[$row->look_id]; 
    if (!isset($look[$row->clothing_article_id])) { 
     //continue this process as needed 
+0

這當然有效,並且是我正在採取的原始方法,但它是很多程序如果/ thens,我想知道是否有更好的方法來做到這一點,即更少的代碼和/或更高效? – Slim 2013-04-26 22:05:21

0

你有加盟級別,每個表一個,全部整理在同一列和編織行一起做許多疑問:

$q1 = "SELECT * FROM look ORDER BY look_id"; 
$q2 = "SELECT look_id, article.* FROM look INNER JOIN article USING(look_id) ORDER BY look_id, article_id"; 
$q3 = "SELECT look_id, article_id, attribute.* FROM look INNER JOIN article USING(look_id) INNER JOIN attribute USING(article_id) ORDER BY look_id, article_id, attribute_id"; 

// loop on looks 
// inner loop on article and add to look 
// inner loop on attribute and add to article 

或者你可以使用現成的ORM庫像doctrine