2013-10-17 72 views
0
SELECT m.manufacturers_id, m.manufacturers_name, 
      op.products_price, 
      op.products_quantity, 
      p.products_cost..... 

我有這些領域,我需要對結果進行算術PHP的,但讓他們分組基於其m.manufacturers_id PHP中的數組。這是我需要或渴望的結果的格式。分組或分類基於其值的特定字段

Array(
    [m.manufacturers_id] 
     [m.manufacturers_name] 
      [brands_total_sales ] 
      (sum of all the products_price * qty for the products in this manufacturers_id) 
      [products] 
       [0] => 
        [op.products_price] = '' 
        [op.products_quantity] = '' 
        [p.products_cost] = '' 
       [1] => 
        [op.products_price] = '' 
        [op.products_quantity] = '' 
        [p.products_cost] = '' 
       [2] => 

然後,重複每個獨特的所有產品manufacturers_id。我猜基本上問題是,是否有一個PHP函數來操縱一系列結果「排序」或「組」到manufacturers_id

在此先感謝,非常感謝您的幫助或建議。

回答

2

php腳本

<pre> 
<?php 

// Data Source 
$db_data = array(
    array('manufacturers_id' => 1, 'manufacturers_name' => 'Some Company', 'products_price' => 1.99, 'products_quantity' => 12, 'products_cost' => 0.49), 
    array('manufacturers_id' => 1, 'manufacturers_name' => 'Some Company', 'products_price' => 1.99, 'products_quantity' => 12, 'products_cost' => 0.49), 
    array('manufacturers_id' => 2, 'manufacturers_name' => 'Another Company', 'products_price' => 12.99, 'products_quantity' => 112, 'products_cost' => 10.49), 
    array('manufacturers_id' => 2, 'manufacturers_name' => 'Another Company', 'products_price' => 12.99, 'products_quantity' => 112, 'products_cost' => 10.49), 
); 

// Sort Data 
$db_data_sorted = array(); 
foreach ($db_data as $data) { 
    $db_data_sorted[$data['manufacturers_id']][$data['manufacturers_name']]['brands_total_sales'] += (intval($data['products_quantity']) * floatval($data['products_price'])); 
    $db_data_sorted[$data['manufacturers_id']][$data['manufacturers_name']]['products'][] = array(
     'products_price' => $data['products_price'], 
     'products_quantity' => $data['products_quantity'], 
     'products_cost' => $data['products_cost'] 
    ); 
} 

// Debug 
print_r($db_data_sorted); 

?> 

輸出:

Array 
(
    [1] => Array 
     (
      [Some Company] => Array 
       (
        [brands_total_sales] => 47.76 
        [products] => Array 
         (
          [0] => Array 
           (
            [products_price] => 1.99 
            [products_quantity] => 12 
            [products_cost] => 0.49 
           ) 

          [1] => Array 
           (
            [products_price] => 1.99 
            [products_quantity] => 12 
            [products_cost] => 0.49 
           ) 

         ) 

       ) 

     ) 

    [2] => Array 
     (
      [Another Company] => Array 
       (
        [brands_total_sales] => 2909.76 
        [products] => Array 
         (
          [0] => Array 
           (
            [products_price] => 12.99 
            [products_quantity] => 112 
            [products_cost] => 10.49 
           ) 

          [1] => Array 
           (
            [products_price] => 12.99 
            [products_quantity] => 112 
            [products_cost] => 10.49 
           ) 

         ) 

       ) 

     ) 

) 
+0

這正是我想要的。爲了獲得每個品牌的總利潤,什麼是最好的方式來解決所有問題,並返回每個品牌的總和(即所有(價格*數量) - (價格*數量))?只是在做foreach的算術? – Aleski

+0

你能告訴我一個預期輸出的例子(比如在你的問題中)你的「總和」之後,我可以提供一個樣本。 – Latheesan

+0

我編輯的主要問題與預期的結果 – Aleski

0

也許是這樣的:

$response = array(); 
while($row = mysql_fetch_array($result)) { 
    $response[$row['m.manufacturers_id']][$row['m.manufacturers_name']][$row['op.products_id']] = array(
    $row['op.products_price'], 
    $row['op.products_quantity'], 
    $row['p.products_cost']); 
}