给定此数组,如何将其重新格式化为这样

时间:2013-10-18 10:19:55

标签: php arrays format

如果我有一个阵列:

$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(每个manufacturer_id)的总和

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

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

非常感谢任何帮助,我感谢任何人花时间回答我相当冗长的问题。我仍然需要重新格式化阵列。

2 个答案:

答案 0 :(得分: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函数用于根据$manufacturers数组中存储的制造商名称查找制造商的ID。您可以更改代码,使其不需要array_search功能,但我这样做是因为传统上您会映射ID-> NAME,而不是NAME-> ID。这只是个人偏好......

有关foreach循环的详细信息,请参阅here

答案 1 :(得分: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