2011-10-20 69 views
0

的PHP & SQL更改SQL結果嵌套在PHP數組中的方式?

  list($since, $start_index, $count) = func_get_args(); 
      $query = "SELECT 
          size_groups.id, size_groups.name, GROUP_CONCAT(size_values.names SEPARATOR ';') AS `display_name`, GROUP_CONCAT(size_values.vals SEPARATOR ';') AS `values`, GROUP_CONCAT(size_count.count SEPARATOR ';') AS `count` 
         FROM size_groups 
         LEFT JOIN 
          (SELECT CONCAT_WS(', ', sizes.size) AS names, CONCAT_WS(', ', sizes.id) AS vals, sizes.id AS ids, sizes.group_id FROM sizes) 
          AS size_values ON size_values.group_id = size_groups.id 
         LEFT JOIN 
          (SELECT CONCAT_WS(', ', COUNT(product_to_sizes.size_id)) AS count, product_to_sizes.size_id FROM product_to_sizes) 
          AS size_count ON size_count.size_id = size_values.ids 
         WHERE size_groups.timestamp >= '%s' AND id = %s 
         GROUP BY size_groups.id 
         ORDER BY size_groups.timestamp ASC LIMIT %s, %s"; 

      return $this->result($this->connection->query($query, $since, $category_id, $start_index, $count)); 

而且

public function result($data) { 

     $result = array(); 
     while ($row = $data->fetch_assoc()) 
      array_push($result, $row); 

     return $result; 

    } 

輸出這樣

Array 
(
    [0] => Array 
     (
      [id] => 21 
      [name] => Sleeve length 
      [display_name] => 33;34;35;36;37;38 
      [values] => 94;95;96;97;98;99 
      [count] => 
     ) 

    [1] => Array 
     (
      [id] => 22 
      [name] => Waist 
      [display_name] => 40;30;32;34;36;38 
      [values] => 105;100;101;102;103;104 
      [count] => 
     ) 
) 

一個數組,但我希望它是這樣

Array 
(
    [0] => Array 
     (
      [id] => 21 
      [name] => Sleeve length 
      [values] => Array(
        Array(
          [display_name] => 33 
          [value] => 94 
          [count] => 5 
         ), 
        Array(
          [display_name] => 34 
          [value] => 95 
          [count] => 31 
         ) 
       ) 
     ), 
     [1] => Array 
      (
       etc 
      ) 
) 

表結構尺寸

+-----+--------+----------+----------+---------------------+ 
| id | size | alt_size | group_id | timestamp   | 
+-----+--------+----------+----------+---------------------+ 
| 81 | Small |   |  19 | 2011-07-15 17:26:01 | 
| 82 | Medium |   |  19 | 2011-07-15 17:26:10 | 
| 83 | Large |   |  19 | 2011-07-15 17:26:14 | 
| 84 | 14.5 |   |  20 | 2011-07-15 17:28:01 | 
| 85 | 15  |   |  20 | 2011-07-15 17:28:05 | 
| 86 | 15.5 |   |  20 | 2011-07-15 17:28:08 | 
| 87 | 16  |   |  20 | 2011-07-15 17:28:13 | 
| 88 | 16.5 |   |  20 | 2011-07-15 17:28:18 | 
| 89 | 17  |   |  20 | 2011-07-15 17:28:21 | 
| 90 | 17.5 |   |  20 | 2011-07-15 17:28:24 | 
| 91 | 18  |   |  20 | 2011-07-15 17:28:27 | 
| 92 | 19  |   |  20 | 2011-07-15 17:28:32 | 
| 93 | 20  |   |  20 | 2011-07-15 17:28:36 | 
| 94 | 33  |   |  21 | 2011-07-15 17:28:49 | 
| 95 | 34  |   |  21 | 2011-07-15 17:28:54 | 
| 96 | 35  |   |  21 | 2011-07-15 17:28:56 | 
| 97 | 36  |   |  21 | 2011-07-15 17:29:00 | 
| 98 | 37  |   |  21 | 2011-07-15 17:29:05 | 
| 99 | 38  |   |  21 | 2011-07-15 17:29:15 | 
| 100 | 30  |   |  22 | 2011-07-15 17:29:42 | 
| 101 | 32  |   |  22 | 2011-07-15 17:29:46 | 
| 102 | 34  |   |  22 | 2011-07-15 17:29:51 | 
| 103 | 36  |   |  22 | 2011-07-15 17:30:03 | 
| 104 | 38  |   |  22 | 2011-07-15 17:30:06 | 
| 105 | 40  |   |  22 | 2011-07-15 17:30:10 | 
| 106 | 32  |   |  23 | 2011-07-15 17:30:52 | 
| 107 | 34  |   |  23 | 2011-07-15 17:30:56 | 
| 108 | 36  |   |  23 | 2011-07-15 17:31:00 | 
| 109 | 38  |   |  23 | 2011-07-15 17:31:04 | 
+-----+--------+----------+----------+---------------------+ 

對於size_groups

+----+---------------+---------------------+ 
| id | name   | timestamp   | 
+----+---------------+---------------------+ 
| 19 | Size   | 2011-07-15 17:25:45 | 
| 20 | Collar size | 2011-07-15 17:26:50 | 
| 21 | Sleeve length | 2011-07-15 17:27:00 | 
| 22 | Waist   | 2011-07-15 17:27:28 | 
| 23 | Leg   | 2011-07-15 17:27:38 | 
+----+---------------+---------------------+ 

而且products_to_sizes

+------+------------+---------+-------------+----------+---------------------+ 
| id | product_id | size_id | stock_level | theorder | timestamp   | 
+------+------------+---------+-------------+----------+---------------------+ 
| 8050 |  683 |  109 |   0 |  0 | 2011-07-16 10:45:30 | 
| 8049 |  683 |  108 |   0 |  0 | 2011-07-16 10:45:30 | 
| 8048 |  683 |  107 |   0 |  0 | 2011-07-16 10:45:30 | 
| 8099 |  679 |  109 |   0 |  0 | 2011-07-16 10:48:30 | 
| 8098 |  679 |  108 |   0 |  0 | 2011-07-16 10:48:30 | 
| 8097 |  679 |  107 |   0 |  0 | 2011-07-16 10:48:30 | 
| 8096 |  679 |  106 |   0 |  0 | 2011-07-16 10:48:30 | 
| 8095 |  679 |  105 |   0 |  0 | 2011-07-16 10:48:30 | 
| 8094 |  679 |  104 |   0 |  0 | 2011-07-16 10:48:30 | 
| 8093 |  679 |  103 |   0 |  0 | 2011-07-16 10:48:30 | 
+------+------------+---------+-------------+----------+---------------------+ 

任何想法?

+0

你知道,如果沒有定製的循環和陣列的構建,你將不能夠有輸出,你所描述的方式? –

+0

你可以發佈你的PHP? – Herb

+0

@Herb PHP添加了 –

回答

1

很容易通過PHP來修復。 假設$項目包含您的記錄陣列...

foreach ($items as $itemkey=>$item) { 
    if ($item['display_name']) { 

     // prep vars 
     $displaynames = explode(';',$item['display_name']); 
     $values = explode(';',$item['values']); 
     $count = explode(';',$item['count']); 
     unset($items[$itemkey]['values']); 

     // iterate 
     $i=0; 
     foreach ($displaynames as $null) { 
      $items[$itemkey]['values'][$i] = array(
       'display_name' => $displaynames[$i], 
       'value'  => $values[$i], 
       'count'  => $count[$i] 
      ); 
      $i++; 
     } 

    } else { 
     $items[$itemkey]['values'] = null; 
    } 
} 
+2

或者更好的是,讓我看看錶'sizes','size_groups','size_values'和'size_count'的表格語法和示例數據。也許有一種只有MySQL的方式。 – neokio

+0

我已經添加了結構 –

+0

酷,我給它一個去。可以肯定的是,當前的MySQL查詢是一個熊,但它的效率非常高。所以我會保持原樣,並使用上面的PHP「在後期修復」,在$ result上執行FOREACH,這會迭代陣列並將其重建爲您的要求。通常最好指定你的MySQL查詢,但有時候PHP是適合工作的正確工具:) – neokio