I have an array like this. it contain three array list and two are same product id here what i want to do , add both price and quantity values in the list and make that to a sing array
Array
(
[0] => Array
(
[order_product_id] => 882
[order_id] => 814
[product_id] => 192
[quantity] => 40
[price] => 410.0000
[total] => 16400.0000
[product_value] => 25
)
[1] => Array
(
[order_product_id] => 881
[order_id] => 815
[product_id] => 200
[quantity] => 20
[price] => 1049.0000
[total] => 20980
[product_value] => 60
)
[2] => Array
(
[order_product_id] => 882
[order_id] => 815
[product_id] => 192
[quantity] => 10
[price] => 410.0000
[total] => 4100.0000
[product_value] => 25
)
)
So here, I want an array like this
Array
(
[0] => Array
(
[order_product_id] => 882
[order_id] => 814
[product_id] => 192
[quantity] => 60
[price] => 410.0000
[total] => 24600.0000
[product_value] => 25
)
[1] => Array
(
[order_product_id] => 881
[order_id] => 815
[product_id] => 200
[quantity] => 20
[price] => 1049.0000
[total] => 20980
[product_value] => 60
)
)
I tried condition code like this but didn't work it properly
foreach ($product as $key => $products)
{
foreach ($product as $keys => $row)
{
if ($products['product_id']==$row['product_id'])
{
}
else
{
}
}
}
so somebody please help me to figure it out please
This should work nicely. Essentially we are giving each array a key based on the id in a new array and over each iteration we check if we've already done the previous and if so we add the price and quantity:
$array = array(
0 => array(
'order_product_id' => 882,
'quantity' => 40,
'price' => 410
),
1 => array(
'order_product_id' => 881,
'quantity' => 20,
'price' => 1049
),
2 => array(
'order_product_id' => 882,
'quantity' => 10,
'price' => 410
)
);
$mod_arr = array();
foreach ($array as $item) {
$id = $item['order_product_id'];
// we've already created this item? yes: add together current item price and quantity with previous
if (isset($mod_arr[$id])) {
$mod_arr[$id]['quantity'] = $mod_arr[$id]['quantity'] + $item['quantity'];
$mod_arr[$id]['price'] = $mod_arr[$id]['price'] + $item['price'];
continue;
}
$mod_arr[$id] = $item;
}
echo '<pre>';
print_r($mod_arr);
Generates:
Array
(
[882] => Array
(
[order_product_id] => 882
[quantity] => 50
[price] => 820
)
[881] => Array
(
[order_product_id] => 881
[quantity] => 20
[price] => 1049
)
)
Related
Having some problem grouping a simple Array. Would like to group sizes and sum quantity. These are shoe sizes.
This is my array:
Array
(
[0] => Array
(
[sku] => '82368-21'
[size] => 36
[quantity] => 1
)
[1] => Array
(
[sku] => '82368-21'
[size] => 36
[quantity] => 3
)
[2] => Array
(
[sku] => '82368-22'
[size] => 38
[quantity] => 0
)
[3] => Array
(
[sku] => '82368-23'
[size] => 39
[quantity] => 2
)
[4] => Array
(
[sku] => '82368-23'
[size] => 39
[quantity] => 1
)
)
As you can see the shoes has multiple sizes and its quantity. There
is no need to remove any duplicates as they should all group by size.
I would like to output the following:
Array
(
[0] => Array
(
[sku] => '82368-21'
[size] => 36
[quantity] => 4
)
[1] => Array
(
[sku] => '82368-22'
[size] => 38
[quantity] => 0
)
[2] => Array
(
[sku] => '82368-23'
[size] => 39
[quantity] => 3
)
)
You can do this with a simple foreach loop:
$array = array (
0 =>
array (
'size' => 36,
'quantity' => 1
),
1 =>
array (
'size' => 36,
'quantity' => 3
),
2 =>
array (
'size' => 38,
'quantity' => 0
),
3 =>
array (
'size' => 39,
'quantity' => 2
),
4 =>
array (
'size' => 39,
'quantity' => 1
)
);
$out = [];
foreach($array as $value){
$key = $value['size'];
if(!isset($out[$key])){
$out[$key] = $value;
}else{
$out[$key]['quantity'] += $value['quantity'];
}
}
print_r($out);
Output
Array
(
[36] => Array
(
[size] => 36
[quantity] => 4
)
[38] => Array
(
[size] => 38
[quantity] => 0
)
[39] => Array
(
[size] => 39
[quantity] => 3
)
)
Sandbox
I think the thing you were missing was using the size as the key. With arrays when you want to group or combine things with a single value match, it's best to take that value and use it as the array key. That just makes it easier to find the group in the output array. Once your done with it you can always use array_values to reset the keys to "normal" numbered indexs.
//reset the array keys
$out = array_values($out);
print_r($out);
Output
Array
(
[0] => Array
(
[size] => 36
[quantity] => 4
)
[1] => Array
(
[size] => 38
[quantity] => 0
)
[2] => Array
(
[size] => 39
[quantity] => 3
)
)
Sandbox
Cheers
I am trying to convert an multi-dimentional array of product data where the PayPal email is set as one of the array values(can be same for some). I need help on merging those as arrays where the paypal_email key is same. Here is the print_r of the array that I have.
Array
(
[0] => Array
(
[name] => Test Product
[product_id] => 307
[variation_id] => 0
[subtotal] => 30
[total] => 30
[quantity] => 2
[subtotal_tax] => 2.66
[total_tax] => 2.66
[paypal_email] => thisismytestpaypal#sandbox.com
[vendor_id] => 2
[shipping] => Array
(
[shipping_total] => 25.00
[shipping_tax] => 2.22
)
)
[1] => Array
(
[name] => Album
[product_id] => 354
[variation_id] => 0
[subtotal] => 15
[total] => 15
[quantity] => 1
[subtotal_tax] => 1.33
[total_tax] => 1.33
[paypal_email] => thisismytestpaypal#sandbox.com
[vendor_id] => 11
[shipping] => Array
(
[shipping_total] => 25.00
[shipping_tax] => 2.22
)
)
[2] => Array
(
[name] => Beanie with Logo
[product_id] => 357
[variation_id] => 0
[subtotal] => 18
[total] => 18
[quantity] => 1
[subtotal_tax] => 1.60
[total_tax] => 1.60
[paypal_email] => ab2#sandbox.com
[vendor_id] => 10
[shipping] => Array
(
[shipping_total] => 25.00
[shipping_tax] => 2.22
)
)
[3] => Array
(
[name] => Long Sleeve Tee
[product_id] => 352
[variation_id] => 0
[subtotal] => 75
[total] => 75
[quantity] => 3
[subtotal_tax] => 6.66
[total_tax] => 6.66
[paypal_email] => ab2#sandbox.com
[vendor_id] => 2
[shipping] => Array
(
[shipping_total] => 25.00
[shipping_tax] => 2.22
)
)
[4] => Array
(
[name] => Polo
[product_id] => 353
[variation_id] => 0
[subtotal] => 80
[total] => 80
[quantity] => 4
[subtotal_tax] => 7.10
[total_tax] => 7.10
[paypal_email] => tv3#sandbox.com
[vendor_id] => 8
[shipping] => Array
(
[shipping_total] => 25.00
[shipping_tax] => 2.22
)
)
[5] => Array
(
[name] => Product From Test Vendor 3
[product_id] => 145
[variation_id] => 0
[subtotal] => 60
[total] => 60
[quantity] => 3
[subtotal_tax] => 5.33
[total_tax] => 5.33
[paypal_email] => tv3#sandbox.com
[vendor_id] => 10
[shipping] => Array
(
[shipping_total] => 25.00
[shipping_tax] => 2.22
)
)
[6] => Array
(
[name] => Product From Vendor 1
[product_id] => 143
[variation_id] => 0
[subtotal] => 20
[total] => 20
[quantity] => 2
[subtotal_tax] => 1.78
[total_tax] => 1.78
[paypal_email] => tv1#sandbox.com
[vendor_id] => 8
[shipping] => Array
(
[shipping_total] => 25.00
[shipping_tax] => 2.22
)
)
)
I need the array to merged as a multi-dimentional array with the paypal email as key and other data as values.. For example,
Array
( [thisismytestpaypal#sandbox.com] => Array
(
[0] => Array
(
[name] => Test Product
[product_id] => 307
[variation_id] => 0
[subtotal] => 30
[total] => 30
[quantity] => 2
[subtotal_tax] => 2.66
[total_tax] => 2.66
[paypal_email] => thisismytestpaypal#sandbox.com
[vendor_id] => 2
[shipping] => Array
(
[shipping_total] => 25.00
[shipping_tax] => 2.22
)
)
[1] => Array
(
[name] => Album
[product_id] => 354
[variation_id] => 0
[subtotal] => 15
[total] => 15
[quantity] => 1
[subtotal_tax] => 1.33
[total_tax] => 1.33
[paypal_email] => thisismytestpaypal#sandbox.com
[vendor_id] => 11
[shipping] => Array
(
[shipping_total] => 25.00
[shipping_tax] => 2.22
)
)
)
[ab2#sandbox.com] => Array
(
[0] => Array
(
[name] => Beanie with Logo
[product_id] => 357
[variation_id] => 0
[subtotal] => 18
[total] => 18
[quantity] => 1
[subtotal_tax] => 1.60
[total_tax] => 1.60
[paypal_email] => ab2#sandbox.com
[vendor_id] => 10
[shipping] => Array
(
[shipping_total] => 25.00
[shipping_tax] => 2.22
)
)
[1] => Array
(
[name] => Long Sleeve Tee
[product_id] => 352
[variation_id] => 0
[subtotal] => 75
[total] => 75
[quantity] => 3
[subtotal_tax] => 6.66
[total_tax] => 6.66
[paypal_email] => ab2#sandbox.com
[vendor_id] => 2
[shipping] => Array
(
[shipping_total] => 25.00
[shipping_tax] => 2.22
)
)
)
[tv3#sandbox.com] => Array
(
[0] => Array
(
[name] => Polo
[product_id] => 353
[variation_id] => 0
[subtotal] => 80
[total] => 80
[quantity] => 4
[subtotal_tax] => 7.10
[total_tax] => 7.10
[paypal_email] => tv3#sandbox.com
[vendor_id] => 8
[shipping] => Array
(
[shipping_total] => 25.00
[shipping_tax] => 2.22
)
)
[1] => Array
(
[name] => Product From Test Vendor 3
[product_id] => 145
[variation_id] => 0
[subtotal] => 60
[total] => 60
[quantity] => 3
[subtotal_tax] => 5.33
[total_tax] => 5.33
[paypal_email] => tv3#sandbox.com
[vendor_id] => 10
[shipping] => Array
(
[shipping_total] => 25.00
[shipping_tax] => 2.22
)
)
)
[tv1#sandbox.com] => Array
(
[0] => Array
(
[name] => Product From Vendor 1
[product_id] => 143
[variation_id] => 0
[subtotal] => 20
[total] => 20
[quantity] => 2
[subtotal_tax] => 1.78
[total_tax] => 1.78
[paypal_email] => tv1#sandbox.com
[vendor_id] => 8
[shipping] => Array
(
[shipping_total] => 25.00
[shipping_tax] => 2.22
)
)
)
)
How can I do that?
Thanks
$orders = [.....]; //your array
$mappedOrders = [];
foreach($orders as $order) {
$mail = $order['paypal_email'];
if(!isset($mappedOrders[$mail])) { $mappedOrders[$mail] = []; } //create new array in map if $mail index not exists
$mappedOrders[$mail][] = $order; // push order into array for mapped array
}
var_dump($mappedOrders);
Do a loop with array key checking
$result = [];
foreach ($array as $value) {
$email = $value['paypal_email']; // Get email
if (!array_key_exists($email, $result)) { // Check if already on the result array
$result[$email] = []; // Create new array with email as key if not
}
$result[$email][] = $value; // push value to email key
}
print_r($result);
Consider first array hold by variable $array. Then iterate it with simple foreach loop and transfer data to new array $newArray with key as value in paypal_email node. Code should be:
$newArray = [];
foreach ($array as $value) {
$newArray[$value['paypal_email']][] = $value;
}
print_r($newArray);
<?php
$data =
[
[
'name' => 'John',
'email' => 'thebeatles#example.com'
],
[
'name' => 'Ringo',
'email' => 'thebeatles#example.com'
],
[
'name' => 'Mick',
'email' => 'thestones#example.com'
],
[
'name' => 'Keith',
'email' => 'thestones#example.com'
],
];
$grouped_by_email = [];
foreach($data as $item)
$grouped_by_email[$item['email']][] = $item;
var_export($grouped_by_email);
Output:
array (
'thebeatles#example.com' =>
array (
0 =>
array (
'name' => 'John',
'email' => 'thebeatles#example.com',
),
1 =>
array (
'name' => 'Ringo',
'email' => 'thebeatles#example.com',
),
),
'thestones#example.com' =>
array (
0 =>
array (
'name' => 'Mick',
'email' => 'thestones#example.com',
),
1 =>
array (
'name' => 'Keith',
'email' => 'thestones#example.com',
),
),
)
please someone will guide me to how to add the values of below conditions and then update the added value in code .
how to sum value of where code =low_order_fee and where code= goods_total then update the sum value in where code=goods_total.
Array
(
[0] => Array
(
[order_total_id] => 999
[order_id] => 194
[code] => goods_total
[title] => Goods-Total
[text] => £130.00
[value] => 130.0000
[sort_order] => 1
)
[1] => Array
(
[order_total_id] => 1000
[order_id] => 194
[code] => low_order_fee
[title] => * Carriage
[text] => £10.00
[value] => 10.0000
[sort_order] => 3
)
[2] => Array
(
[order_total_id] => 1001
[order_id] => 194
[code] => sub_total
[title] => Sub-Total
[text] => £130.00
[value] => 130
[sort_order] => 4
)
[3] => Array
(
[order_total_id] => 1002
[order_id] => 194
[code] => tax
[title] => VAT (20%)
[text] => £26.00
[value] => 26.0000
[sort_order] => 5
)
[4] => Array
(
[order_total_id] => 1003
[order_id] => 194
[code] => total
[title] => Invoice Total
[text] => £166.00
[value] => 166.0000
[sort_order] => 9
)
)
A rudimentary solution:
foreach ($array_var as $key => $item) {
if ($item['code'] == 'low_order_fee') {
$first_val = $item['value'];
}
if ($item['goods_total'] == 'low_order_fee') {
$sec_val = $item['value'];
$position = $key;
}
}
$array_var[$position]['goods_total'] = $first_val + $sec_val;
But maybe you should think about store the values in another way to make easier access to them.
I hope it helps.
How to sum array values if products id same? I need sum product_price, quantity, total_price, shipping_price. This is my example
[936] => Array
(
[order_number] => 936
[status] => cancelled
[products] => Array
(
[0] => Array
(
[product_id] => 19
[sku] => sku2222222
[product_price] => 12.00
[quantity] => 1
[total_price] => 17
[shipping_price] => 5.00
)
[1] => Array
(
[product_id] => 19
[sku] => sku2222222
[product_price] => 12.00
[quantity] => 1
[total_price] => 17
[shipping_price] => 5.00
)
[2] => Array
(
[product_id] => 5
[sku] => sku2222222
[product_price] => 12.00
[quantity] => 1
[total_price] => 17
[shipping_price] => 5.00
)
)
)
You need use for loop to sum all needed variables.
$orders = array(
936 => array(
"order_number" => 936,
"products" => array(
array(
"product_id" => 19,
"total_price" => 17,
"shipping_price" => 10
),
array(
"product_id" => 19,
"total_price" => 17,
"shipping_price" => 10
),
array(
"product_id" => 5,
"total_price" => 17,
"shipping_price" => 10
),
)
)
);
foreach ($orders as $order_id => $order) {
$order_products = array();
foreach ($order['products'] as $product) {
if (isset($order_products[$product['product_id']])) {
$order_products[$product['product_id']]['total_price'] += $product['total_price'];
$order_products[$product['product_id']]['shipping_price'] += $product['shipping_price'];
} else {
$order_products[$product['product_id']] = $product;
}
}
$orders[$order_id]['products'] = $order_products;
}
print_r($orders);
I need your help to solve this :)
i've this array
Array (
[0] => Array ( [order_id] => 121 [item_id] => 4344 [item_name] => Product [item_price] => 123 [item_type] => product [paypal_address] => email#test.com [qty] => 4 [currency] => EUR )
[1] => Array ( [order_id] => 121 [item_id] => 3444 [item_name] => Product1 [item_price] => 444 [item_type] => product [paypal_address] => email#test.com [qty] => 2 [currency] => EUR )
[2] => Array ( [order_id] => 121 [item_id] => 1233 [item_name] => Product2 [item_price] => 120 [item_type] => product [paypal_address] => email2#test.com [qty] => 18 [currency] => EUR )
)
I would like to loop on it and group them by values into a new array.
For example:
Pick all items in array that have the same paypal_address sum price, sum qty and move it into new array.
This is what i want to achieve, any tip / suggestion ?
EDIT:
At the end i want an array like this or similar:
Array (
[0] => Array ( [order_id] => 121 [items_id] => array(4344, 3444) [items_name] => 'Product , Product1' [amt] => 567 [item_type] => product [paypal_address] => email#test.com [qty] => 8 [currency] => EUR )
[1] => Array ( [order_id] => 121 [items_id] => 1233 [items_name] => Product2 [amt] => 120 [item_type] => product [paypal_address] => email2#test.com [qty] => 18 [currency] => EUR )
)
EDIT2:
what i did so far. but it doesn't work well and is not good to read.
$groupedParams = array();
foreach($params as $key=>$param){
if(!array_key_exists($param['paypal_address'], $groupedParams) && $param['item_type'] == 'product'){
$groupedParams[$param['paypal_address']] = array(
'order_id' => $param['order_id'],
'item_id' => $param['item_id'],
'item_name' => $param['item_name'],
'qty' => $param['qty'],
'amt' => $param['item_price'],
'item_type' => $param['item_type']
);
}else if(array_key_exists($param['paypal_address'], $groupedParams) && $param['item_type'] == 'product'){
$newItemId = $groupedParams[$param['paypal_address']]['item_id'].','.$param['item_id'];
$newAmt = (int)$groupedParams[$param['paypal_address']]['amt']+(int)$param['item_price'];
$newQty = (int)$groupedParams[$param['paypal_address']]['qty']+(int)$param['qty'];
$newItemName = (string)$groupedParams[$param['paypal_address']]['item_name'].' - '.(int)$param['item_name'];
$groupedParams[$param['paypal_address']] = array(
'order_id' => $param['order_id'],
'item_id' => $newItemId,
'item_name' => $newItemName,
'qty' => $newQty,
'amt' => $newAmt,
'item_type' => $param['item_type']
);
}
}
Thanks
You want to reorganise it to a cleaner format from what I understand, simply put:
$array = array();
foreach($params as $ar) {
$array[$ar['paypal_address']][] = $ar;
}
Example: http://jdl-enterprises.co.uk/sof/25767688.php
Get Array and its values
make unique key on paypal_address
Use unique key to create temp array
Store all values with respective unique key in temp array
$arr = Array (
'0' => Array ( 'order_id' => 121 ,'item_id' => 4344 ,'item_name' => 'Product' ,'item_price' => 123 ,'item_type' => 'product' ,'paypal_address' => 'email#test.com' ,'qty' => 4 ,'currency' => 'EUR' ) ,
'1' => Array ( 'order_id' => 121 ,'item_id' => 3444 ,'item_name' => 'Product1' ,'item_price' => 444 ,'item_type' => 'product' ,'paypal_address' => 'email#test.com' ,'qty' => 2 ,'currency' => 'EUR' ) ,
'2' => Array ( 'order_id' => 121 ,'item_id' => 1233 ,'item_name' => 'Product2' ,'item_price' => 120 ,'item_type' => 'product' ,'paypal_address' => 'email2#test.com' ,'qty' => 18 ,'currency' => 'EUR' )
);
$tmpArr = Array();
$cnt = sizeof($arr);
for($i=0;$i<$cnt;$i++){
$paypal_address = $arr[$i]['paypal_address'];
if(!is_array($tmpArr[$paypal_address])){
$tmpArr[$paypal_address] = Array();
}
$tmpArr[$paypal_address]['paypal_address'] = $arr[$i]['paypal_address'];
$tmpArr[$paypal_address]['order_id'] = $arr[$i]['order_id'];
$tmpArr[$paypal_address]['item_name'][] = $arr[$i]['item_name'];
$tmpArr[$paypal_address]['item_type'][] = $arr[$i]['item_type'];
$tmpArr[$paypal_address]['currency'][] = $arr[$i]['currency'];
$tmpArr[$paypal_address]['qty'] = isset($tmpArr[$paypal_address]['qty']) ? $tmpArr[$paypal_address]['qty'] + $arr[$i]['qty'] : $arr[$i]['qty'];
$tmpArr[$paypal_address]['item_id'] = $arr[$i]['item_id'];
$tmpArr[$paypal_address]['item_id'][] = $item_id;
$tmpArr[$paypal_address]['item_price'] = isset($tmpArr[$paypal_address]['item_price']) ? $tmpArr[$paypal_address]['item_price'] + $arr[$i]['item_price'] : $arr[$i]['item_price'];
}
print_r($tmpArr);
Output of the array is as below:
Array([email#test.com] => Array ([paypal_address] => email#test.com[order_id] => 121[item_name] => Array ( [0] => Product [1] => Product1 )[item_type] => Array ( [0] => product [1] => product )[currency] => Array ( [0] => EUR [1] => EUR )[qty] => 6[item_id] => 3444[item_price] => 567 )[email2#test.com] => Array ([paypal_address] => email2#test.com[order_id] => 121[item_name] => Array ( [0] => Product2 )[item_type] => Array ( [0] => product )[currency] => Array ( [0] => EUR )[qty] => 18[item_id] => 1233[item_price] => 120 ))
Note: Make changes in code according to your requirement
$details =Array (
[0] => Array ( [order_id] => 121 [item_id] => 4344 [item_name] => Product [item_price] => 123 [item_type] => product [paypal_address] => email#test.com [qty] => 4 [currency] => EUR )
[1] => Array ( [order_id] => 121 [item_id] => 3444 [item_name] => Product1 [item_price] => 444 [item_type] => product [paypal_address] => email#test.com [qty] => 2 [currency] => EUR )
[2] => Array ( [order_id] => 121 [item_id] => 1233 [item_name] => Product2 [item_price] => 120 [item_type] => product [paypal_address] => email2#test.com [qty] => 18 [currency] => EUR )
)
$sorted = array();
foreach ($details as $key => $value) {
$add = $value['paypal_address'];
foreach ($details as $index => $detail) {
if ($add == $detail['paypal_address']) {
foreach ($detail as $cat => $val) {
if (!in_array($val, $sorted[$key][$cat])) {
$sorted[$key][$cat][] = $val;
}
}
unset($details[$index]);
}
}
}
// Output array
print_r($sorted);