1

如果我有一个数组:

$data = array(
    array(
        'manufacturers_id' => 29,
        'manufacturers_name' => 'Quicksilver',
        'products_quantity' => 1,
        'products_price' => 15.6000,
        'products_cost' => 8.0000,
    ),
    array(
        'manufacturers_id' => 29,
        'manufacturers_name' => 'Quicksilver',
        'products_quantity' => 2,
        'products_price' => 4.6722,
        'products_cost' => 2.4000,
    )
);

我怎样才能重新格式化它以提供结构

Array
 (
 [Quiksilver] => Array
    (
        [brands_sales] => $brandsSalesVal
        [brands_products_sold] => $brandsSoldVal
        [brands_costs] => $brandsCostsVal
    )

$brandsSalesVal= 所有 products_price * products_quantity 的总和(对于每个制造商 ID)

$brandsSoldVal= 每个制造商的所有 products_quantity 的总和

$brandsCostsVal= 每个制造商的所有 products_costs 的总和

非常感谢任何帮助,我感谢任何人花时间回答我相当冗长的查询。我仍在处理重新格式化阵列。

4

2 回答 2

2

您需要使用foreach这样的循环:

// Declare the totals array and
// the manufacturers->id map
$totals = array();
$manufacturers = array();

// Loop through the array of products and populate
// the totals array
foreach($data as $value){

    // Set the key to the manufacturers name
    $key = $value['manufacturers_name'];

    // If the array has not been built yet, then ensure the
    // values are set to 0 and add the manufacturer to the 
    // manufacturers map if it is not already there
    if(!isset($totals[$key])){
        // Add the manufacturer to the map
        $manufacturers[$value['manufacturers_id']] = $key;
        // Default the values to 0
        $totals[$key]['brand_sales'] = 0;
        $totals[$key]['brands_products_sold'] = 0;
        $totals[$key]['brands_costs'] = 0;
    }

    // Calculate the brand sales
    $totals[$key]['brand_sales'] += ($value['products_price']*$value['products_quantity']);

    // Calculate the brand sales
    $totals[$key]['brands_products_sold'] += $value['products_quantity'];

    // Calculate the brand sales
    $totals[$key]['brands_costs'] += $value['products_cost'];
}

为了访问存储在上面生成的数组中的信息,您可以使用另一个foreach循环,如下所示:

// Loop through the $totals array and print the result
foreach($totals as $key => $value){

    // Print the manufacturers name and ID
    echo "\n".$key." (ID: ".array_search($key,$manufacturers).")";

    // Print the totals for the current manufacturer
    echo "\n\tBrand Sales: ".$values['brand_sales'];
    echo "\n\tBrand Products Sold: ".$values['brands_products_sold'];
    echo "\n\tBrand Costs: ".$values['brands_costs'];
}

该函数用于根据存储在数组array_search中的制造商名称查找制造商的 ID 。$manufacturers您可以更改代码以使其不需要该array_search功能,但我这样做是因为传统上您将映射 ID->NAME,而不是 NAME->ID。这只是个人喜好...

有关foreach循环的更多信息,请参见此处

于 2013-10-18T10:27:29.923 回答
0
$data = array(
    array(
        'manufacturers_id' => 29,
        'manufacturers_name' => 'Quicksilver',
        'products_quantity' => 1,
        'products_price' => 15.6000,
        'products_cost' => 8.0000,
    ),
    array(
        'manufacturers_id' => 29,
        'manufacturers_name' => 'Quicksilver',
        'products_quantity' => 2,
        'products_price' => 4.6722,
        'products_cost' => 2.4000,
    )
    ,
    array(
        'manufacturers_id' => 30,
        'manufacturers_name' => 'Different Brand',
        'products_quantity' => 2,
        'products_price' => 4.6722,
        'products_cost' => 2.4000,
    )
);

$sortedData = array();
foreach($data as $num => $row){
    $manufacturersName = $row['manufacturers_name'];
    //If we don't have an array made for the manufacturer yet, make one
    if(!isset($sortedData[$manufacturersName])){
        $sortedData[$manufacturersName] = array(
            'brands_sales' => 0,
            'brands_products_sold' => 0,
            'brands_costs' => 0
        );
    };
    //Make a reference to the relevant manufacturer sorted data
    $manufacturerData = &$sortedData[$manufacturersName];
    $qty = $row['products_quantity'];
    //
    $manufacturerData['brands_sales']           += $qty * $row['products_price'];
    $manufacturerData['brands_products_sold']   += $qty;
    $manufacturerData['brands_costs']           += $row['products_cost'];
}

var_dump($sortedData); // <- your result. 

结果:

array (size=2)
  'Quicksilver' => 
    array (size=3)
      'brands_sales' => float 24.9444
      'brands_products_sold' => int 3
      'brands_costs' => float 10.4
  'Different Brand' => 
    array (size=3)
      'brands_sales' => float 9.3444
      'brands_products_sold' => int 2
      'brands_costs' => float 2.4
于 2013-10-18T10:29:46.090 回答