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);
Related
I have two multiple select input result that i want to join. Its about delivery destination and delivery fee.
here is my array result :
Array
(
[destination] => Array
(
[0] => London
[1] => Liverpool
[2] => Nottingham
[3] => Oxford
)
[fee] => Array
(
[0] => 10
[1] => 15
[2] => 20
[3] => 25
)
)
And I want to push these value to each array :
$status = "1";
Result I expected is :
Array
(
[0] => Array
(
[destination] => London
[fee] => 10
[status] => 1
)
[1] => Array
(
[destination] => Liverpool
[fee] => 15
[status] => 1
)
[2] => Array
(
[destination] => Nottingham
[fee] => 20
[status] => 1
)
[3] => Array
(
[destination] => Oxford
[fee] => 25
[status] => 1
)
)
Thanks for helping me.
$array = ['destination' => ['London', 'Liverpool', 'Nottingham', 'Oxford'], 'fee' => [10, 15, 20, 25]];
$result = [];
foreach ($array['destination'] as $index => $value)
{
$result[] = ['destination' => $value, 'fee' => $array['fee'][$index], 'status' => 1];
}
Try this: https://3v4l.org/004PF
<?php
$givenArray = [
'destination' => [
'London',
'Liverpool',
'Nottingham',
'Oxford',
],
'fee' => [
10,
15,
20,
25
],
];
$output = [];
foreach ($givenArray['destination'] as $key => $destination) {
$fee = $givenArray['fee'][$key];
$output[] = [
'destination' => $destination,
'fee' => $fee,
'status' => 1,
];
}
print_r($output);
Output is:
Array
(
[0] => Array
(
[destination] => London
[fee] => 10
[status] => 1
)
[1] => Array
(
[destination] => Liverpool
[fee] => 15
[status] => 1
)
[2] => Array
(
[destination] => Nottingham
[fee] => 20
[status] => 1
)
[3] => Array
(
[destination] => Oxford
[fee] => 25
[status] => 1
)
)
I have 2 arrays category and product_to_category (Structure shown below)
[category] => Array
(
[0] => 299
[2] => 342
[3] => 134
[4] => 333
[5] => 347
[9] => 296
)
And
[product_to_category] => Array
(
[0] => Array
(
[product_id] => 23895
[category_id] => 296
[relevance] => 77.73432159423828
)
[1] => Array
(
[product_id] => 17218
[category_id] => 296
[relevance] => 77.73432159423828
)
[2] => Array
(
[product_id] => 23896
[category_id] => 347
[relevance] => 77.73432159423828
)
....So On (5000+ records)
i have almost 5000+ records in product_to_category array, But i want to pick only 3 products from P2C array per Category. For Example category_id=299 so 3 products of 299 will be added to my new array and same for other categories.
Is there any possible way to do that .
Try this query to fetch record you wanted,
select p2c.product_id, p2c.category_id, p2c.relevance
from category c
LEFT JOIN product_to_category p2c ON p2c.category_id = category.category_id
GROUP BY p2c.product_id, p2c.category_id
HAVING count(*) <= 3
ORDER BY category_id, product_id
You can refer table category to category table and product_to_category to your table name in database.
$category = [0 => 299, 2 => 342, 3 => 134, 4 => 333, 5 => 347, 9 => 296];
$product_to_category = Array
(
0 => Array
(
'product_id' => 23895,
'category_id' => 296,
'relevance' => 77.73432159423828
),
1 => Array
(
'product_id' => 17218,
'category_id' => 296,
'relevance' => 77.73432159423828
),
2 => Array
(
'product_id' => 23896,
'category_id' => 347,
'relevance' => 77.73432159423828
),
3 => Array
(
'product_id' => 23897,
'category_id' => 296,
'relevance' => 77.73432159423828
),
4 => Array
(
'product_id' => 23899,
'category_id' => 296,
'relevance' => 77.73432159423828
),
5 => Array
(
'product_id' => 23894,
'category_id' => 347,
'relevance' => 77.73432159423828
),
6 => Array
(
'product_id' => 23892,
'category_id' => 347,
'relevance' => 77.73432159423828
),
7 => Array
(
'product_id' => 23833,
'category_id' => 347,
'relevance' => 77.73432159423828
)
);
$new_product_to_category = [];
foreach ($category as $categories) {
$count_category_ids = [];
$i=0;
foreach ($product_to_category as $product_to_categories){
if ($categories == $product_to_categories['category_id']){
$count_category_ids[] = $product_to_categories['category_id'];
if (count($count_category_ids) < 4) {
$new_product_to_category[$i]['product_id'] = $product_to_categories['product_id'];
$new_product_to_category[$i]['category_id'] = $product_to_categories['category_id'];
$new_product_to_category[$i]['relevance'] = $product_to_categories['relevance'];
}
}
$i++;
}
}
echo "<pre>";
print_r($new_product_to_category);
Output shows like this :
Array
(
[2] => Array
(
[product_id] => 23896
[category_id] => 347
[relevance] => 77.734321594238
)
[5] => Array
(
[product_id] => 23894
[category_id] => 347
[relevance] => 77.734321594238
)
[6] => Array
(
[product_id] => 23892
[category_id] => 347
[relevance] => 77.734321594238
)
[0] => Array
(
[product_id] => 23895
[category_id] => 296
[relevance] => 77.734321594238
)
[1] => Array
(
[product_id] => 17218
[category_id] => 296
[relevance] => 77.734321594238
)
[3] => Array
(
[product_id] => 23897
[category_id] => 296
[relevance] => 77.734321594238
)
)
$categories = array(6, 5, 3, 4, 2, 1);
$products = array(
array('product_id' => 231, 'category_id' => 1, 'relevance' => 321),
array('product_id' => 232, 'category_id' => 4, 'relevance' => 322),
array('product_id' => 233, 'category_id' => 2, 'relevance' => 323),
array('product_id' => 234, 'category_id' => 4, 'relevance' => 324),
array('product_id' => 235, 'category_id' => 4, 'relevance' => 325),
array('product_id' => 236, 'category_id' => 2, 'relevance' => 326),
array('product_id' => 237, 'category_id' => 1, 'relevance' => 327),
array('product_id' => 238, 'category_id' => 4, 'relevance' => 328),
array('product_id' => 239, 'category_id' => 1, 'relevance' => 329),
array('product_id' => 240, 'category_id' => 1, 'relevance' => 330)
);
$categoryWiseProduct = array();
foreach ($products as $p) {
foreach ($categories as $c) {
if ($c == $p['category_id']) {
if (isset($categoryWiseProduct[$c]) && count($categoryWiseProduct[$c]) > 2) {
break;
}
$categoryWiseProduct[$c][] = $p;
}
}
}
echo'<pre>';print_r($categoryWiseProduct);die;
Output will be:
Array
(
[1] => Array
(
[0] => Array
(
[product_id] => 231
[category_id] => 1
[relevance] => 321
)
[1] => Array
(
[product_id] => 237
[category_id] => 1
[relevance] => 327
)
[2] => Array
(
[product_id] => 239
[category_id] => 1
[relevance] => 329
)
)
[4] => Array
(
[0] => Array
(
[product_id] => 232
[category_id] => 4
[relevance] => 322
)
[1] => Array
(
[product_id] => 234
[category_id] => 4
[relevance] => 324
)
[2] => Array
(
[product_id] => 235
[category_id] => 4
[relevance] => 325
)
)
[2] => Array
(
[0] => Array
(
[product_id] => 233
[category_id] => 2
[relevance] => 323
)
[1] => Array
(
[product_id] => 236
[category_id] => 2
[relevance] => 326
)
)
)
try this code
$collection = array();
foreach($arr as $key => $value){
if(
isset($collection[$value['category_id']])
&&
count($collection[$value['category_id']]) >= 3
) continue;
$collection[$value['product_id']][] = $value['product_id'];
}
In this way, you will get 3 products from each category.
You can add more condition if you need, like check the product id you can use inside if condition
&& in_array($product_id, $collection[$value['category_id']])
I have a below array:
[product] => Array
(
[0] => Array
(
[qty] => 1
[code] => 147818
[price] => 11
[name] => Product1
)
[1] => Array
(
[qty] => 2
[code] => 147818
[price] => 11
[name] => Product1
)
[2] => Array
(
[qty] => 1
[code] => 567432
[price] => 31
[name] => Product2
)
)
I want to add quantities if the code is same. That is, I want the resulting array to be:
[product] => Array
(
[0] => Array
(
[qty] => 3
[code] => 147818
[price] => 11
[name] => Product1
)
[1] => Array
(
[qty] => 1
[code] => 567432
[price] => 31
[name] => Product2
)
)
It should merge the elements only if the code is same. How can I achieve this?
Try this code, it merge and sum the qty by code
$products = [
[
'qty' => 1,
'code' => 147818,
'price' => 11,
'name' => 'Product1'
],
[
'qty' => 2,
'code' => 147818,
'price' => 11,
'name' => 'Product1'
],
[
'qty' => 1,
'code' => 567432,
'price' => 31,
'name' => 'Product2'
],
];
$output = [];
for ($i=0; $i<count($products); $i++) {
if ($output[$products[$i]['code']]['code'] == $products[$i]['code']) {
$output[$products[$i]['code']]['qty'] += $products[$i]['qty'];
}else{
$output[$products[$i]['code']] = $products[$i];
}
}
$output = array_values($output);
print_r($output);
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
)
)
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);