在PHP中,根据特定字段的值对数组进行分组或排序

在PHP中,根据特定字段的值对数组进行分组或排序,php,arrays,Php,Arrays,我有这些字段,需要对结果执行php算法,但要根据它们的m.id对它们进行分组。这是我需要或想要的结果的格式 SELECT m.manufacturers_id, m.manufacturers_name, op.products_price, op.products_quantity, p.products_cost..... 然后,对于每个唯一的制造商\u id的所有产品都会重复此操作。我想基本上问题是,是否有一个php函数可

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

SELECT    m.manufacturers_id, m.manufacturers_name,
          op.products_price,
          op.products_quantity,
          p.products_cost.....
然后,对于每个唯一的
制造商\u id
的所有产品都会重复此操作。我想基本上问题是,是否有一个php函数可以操纵结果数组,将其“排序”或“分组”到
manufacturers\u id

提前感谢,非常感谢您的帮助或建议。

php脚本


可能是这样的:

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
                                )

                        )

                )

        )

)

这正是我想要的。什么是最好的方法,可以将它们全部相加,然后只返回每个品牌的总和(也就是所有(价格*数量)-(价格*数量),以获得每个品牌的总利润)?只是在foreach中进行算术运算?你能给我一个例子,说明你对“和”的期望输出(如你的问题),我可以提供一个示例。
<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
                                )

                        )

                )

        )

)
$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']);
}