douxiduan8344
2013-10-17 13:28
浏览 125
已采纳

基于特定字段的值在PHP中对数组进行分组或排序

SELECT    m.manufacturers_id, m.manufacturers_name,
          op.products_price,
          op.products_quantity,
          p.products_cost.....

I have these fields and I need to perform php arithmetic on the results, but keep them grouped based on their m.manufacturers_id. This is the format of the result I need or desire.

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] =>

Then that repeats for all the products per unique manufacturers_id. I guess basically the question is, is there a php function to manipulate an array of results to "sort" or "group" into manufacturers_id

Thanks in advance, an help or advise is greatly appreciated.

图片转代码服务由CSDN问答提供 功能建议

  SELECT m.manufacturers_id,m.manufacturers_name,
 op.products_price,
 op.products_quantity,  
 p.products_cost ..... 
   
 
 

我有这些字段,我需要对结果执行php算法,但要根据它们的分组进行分组 代码> m.manufacturers_id 。 这是我需要或想要的结果的格式。

  Array(
 [m.manufacturers_id] 
 [m.manufacturers_name] 
 [brands_total_sales] 
( 所有products_price *数量为此manufacturer_id产品的总和
 [产品] 
 [0] => 
 [op.products_price] =''
 [op.products_quantity] =''
 [  p.products_cost] =''
 [1] => 
 [op.products_price] =''
 [op.products_quantity] =''
 [p.products_cost] =''
 [2]  => 
   
 
 

然后对每个唯一的 manufacturer_id 重复所有产品。我猜基本上问题是,是否有PHP函数 操作一系列结果以“排序”或“分组”到 manufacturers_id

在此先感谢帮助或建议,我们非常感谢。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douqiao1997 2013-10-17 13:34
    已采纳

    php script

    <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);
    
    ?>
    

    output:

    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
                                    )
    
                            )
    
                    )
    
            )
    
    )
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doujie4787 2013-10-17 13:37

    Maybe something like :

    $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']);
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题