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

나는 이러한 필드를 가지고 있고 그룹화 자신의 m.manufacturers_id에 따라 결과에 PHP 연산을 수행하지만, 그것들을 유지할 필요가 자사 값을 기반으로 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을 반복합니다. 기본적으로 질문은 "sort"또는 "group"에 결과 배열을 조작하는 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에서 방금 arithmatic을하는 것입니까? – 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']); 
}