Aleski
Aleski

Reputation: 1442

Given this array, how can I reformat it to look like this

If I had an array:

$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,
    )
);

How can I reformat this to provide the structure

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

$brandsSalesVal = A sum of all the products_price * products_quantity (for each manufacturer_id)

$brandsSoldVal= A sum of all the products_quantity for each manufacturer

$brandsCostsVal= A sum of all the products_costs for each manufacturer

Any help is greatly appreciated and I am thankful for anyone to take time to answer my rather lengthy query. I am still getting to grips with reformating arrays.

Upvotes: 1

Views: 64

Answers (2)

Ben Carey
Ben Carey

Reputation: 16948

You need to use a foreach loop like so:

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

In order to access the information stored in the array generated above, you can use another foreach loop like so:

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

The array_search function is used to look up the ID of the manufacturer based on the manufacturers name stored in the $manufacturers array. You can alter the code so that it does not need the array_search function, but I have done it like this because traditionally you would map the ID->NAME, not NAME->ID. It is just personal preference...

For more information on the foreach loop, see here

Upvotes: 2

1owk3y
1owk3y

Reputation: 1202

$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. 

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

Upvotes: 0

Related Questions