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);
Related
I have two arrays like this:
Array1
$array1 = Array
(
0 => Array
(
'ID' => 101,
'Code' => 1075,
'Date' => '2012-03-03 17:13:12.433'
),
1 => Array
(
'ID' => 103,
'Code' => 175,
'Date' => '2012-09-05 20:30:02.217'
),
2 => Array
(
'ID' => 109,
'Code' => 178,
'Date' => '2012-07-05 20:30:02.217'
)
);
Array2
$array2 = Array
(
0 => Array
(
'Amount' => 1234,
'ID' => 101
),
1 => Array
(
'Amount' => 5656,
'ID' => 101
),
2 => Array
(
'Amount' => 1342,
'ID' => 103
),
3 => Array
(
'Amount' => 0,
'ID' => 0
)
);
I'm using the code below to perform a join on the two arrays :
$arr2 = array_column($array2, "ID");
$finalArray = array();
foreach($array1 as $arr){
$key = array_search($arr['ID'], $arr2);
if($key ===false){
$key = array_search(0, $arr2);
$array2[$key]['Found'] = "No";
}
else {
$array2[$key]['Found'] = "Yes";
}
unset($array2[$key]['ID']);
$finalArray[] = array_merge($arr,$array2[$key]);
}
print_r($finalArray);
The current output using the code above is :
finalArray
Array
(
[0] => Array
(
[ID] => 101
[Code] => 1075
[Date] => 2012-03-03 17:13:12.433
[Amount] => 1234 //considers only the first entry of ID 101 in array2
[Found] => Yes
)
[1] => Array
(
[ID] => 103
[Code] => 175
[Date] => 2012-09-05 20:30:02.217
[Amount] => 1342
[Found] => Yes
)
[2] => Array
(
[ID] => 109
[Code] => 178
[Date] => 2012-07-05 20:30:02.217
[Amount] => 0
[Found] => No
)
)
But since in array2 there are two entries for ID 101 but the code above only takes the first match for a matching ID.
The expected output is :
Desired Output
Array
(
[0] => Array
(
[ID] => 101
[Code] => 1075
[Date] => 2012-03-03 17:13:12.433
[Amount] => 6890 //sum of all the amounts(1234+5656)for matching ID 101
[Found] => Yes
)
[1] => Array
(
[ID] => 103
[Code] => 175
[Date] => 2012-09-05 20:30:02.217
[Amount] => 1342
[Found] => Yes
)
[2] => Array
(
[ID] => 109
[Code] => 178
[Date] => 2012-07-05 20:30:02.217
[Amount] => 0
[Found] => No
)
)
I'm not able to figure out how to do the addition here.
The code should do addition of the Amount feild for each matching ID of array2 and merge that amount to array1 Amount feild as shown in the expected output above.
How do I modify my current code such that it gives me the desired output?
What about:
$finalArray = array();
foreach ($array1 as $arr1)
{
$amount = 0;
foreach ($array2 as $key2 => $arr2)
{
if ($arr1['ID'] === $arr2['ID'])
{
$amount += $arr2['Amount'];
unset($array2[$key2]);
}
}
$finalArray[] = array_merge($arr1, array(
'Amount' => $amount,
'Found' => $amount ? "Yes" : "No"
));
}
print_r($finalArray);
Output:
Array
(
[0] => Array
(
[ID] => 101
[Code] => 1075
[Date] => 2012-03-03 17:13:12.433
[amount] => 6890
[found] => Yes
)
[1] => Array
(
[ID] => 103
[Code] => 175
[Date] => 2012-09-05 20:30:02.217
[amount] => 1342
[found] => Yes
)
[2] => Array
(
[ID] => 109
[Code] => 178
[Date] => 2012-07-05 20:30:02.217
[amount] => 0
[found] => No
)
)
Let me know if you need any further explanation.
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 have converted xml string into php array. But the requirement is something different. Following is an array which is created using XML string by following
$xml = simplexml_load_string($mystin, "SimpleXMLElement",
LIBXML_NOCDATA); $json = json_encode($xml); $array =
json_decode($json,TRUE);
Array
(
[0] => Array
(
[data] => Array
(
[0] => Array
(
[attribute_set] => default
[description] => productdescription
[is_in_stock] => 1
[meta_description] => Product meta description
[meta_keyword] => Product meta keyword
[meta_title] => Product meta title
[name] => myproduct6
[price] => 100
[qty] => 1000
[re_skus] => sdfefwef
[short_description] => this is a short descriptio
[sku] => myproduct_surabhi7
[status] => 1
[store] => admin
[tax_class_id] => 4
[type] => simple
[url_key] => my-product
[url_path] => my-product.html
[visibility] => 4
[weight] => 10
)
[1] => Array
(
[attribute_set] => default
[description] => productdescription
[is_in_stock] => 1
[meta_description] => Product meta description
[meta_keyword] => Product meta keyword
[meta_title] => Product meta title
[name] => myproduct6
[price] => 100
[qty] => 1000
[re_skus] => sdfefwef
[short_description] => this is a short descriptio
[sku] => myproduct_surabhi7
[status] => 1
[store] => admin
[tax_class_id] => 4
[type] => simple
[url_key] => my-product
[url_path] => my-product.html
[visibility] => 4
[weight] => 10
)
)
)
)
I need the above array as following array:
Array
(
[0] => Array
(
[0] => Array
(
[name] => Product1
[sku] => Product1
[description] => Product description
[short_description] => Product short description
[weight] => 10
[status] => 1
[url_key] => wat12
[url_path] => wat12.html
[visibility] => 4
[price] => 100
[tax_class_id] => 4
[meta_title] => Product meta title
[meta_keyword] => Product meta keyword
[meta_description] => Product meta description
[store] => admin
[attribute_set] => default
[type] => simple
[is_in_stock] => 1
[color] => Orange
[re_skus] => Testsimple2,Testsimple1
)
)
[1] => Array
(
[1] => Array
(
[name] => Product2
[sku] => Product2
[description] => Product description
[short_description] => Product short description
[weight] => 10
[status] => 1
[url_key] => wat12
[url_path] => wat12.html
[visibility] => 4
[price] => 100
[tax_class_id] => 4
[meta_title] => Product meta title
[meta_keyword] => Product meta keyword
[meta_description] => Product meta description
[store] => admin
[attribute_set] => default
[type] => simple
[is_in_stock] => 1
[color] => Orange
[re_skus] => Testsimple2,Testsimple1
)
)
)
Try this may be it help
$array = array(
0 => array(
'data' => array(0 => array('abc2' => 1, 'adsg' =>2),1=>array('abc' => 1, 'adsg4' =>2)))
);
$new_data = array();
foreach ($array as $row)
{
for($i=0; $i<count($row['data']); $i++){
$newArray = array();
$newArray[$i] = $row['data'][$i];
$new_data[] = $newArray;
}
}
print_r($new_data);
Have a look at this example:
$array = array(
98 => array(
'City' => 'Caracas',
'Country' => 'Venezuela',
'Continent' => 'Latin America',
),
99 => array(
'City' => 'Cairo',
'Country' => 'Egypt',
'Continent' => 'Middle East',
),
105 => array(
'City' => 'Abu Dhabi',
'Country' => 'United Arab Emirates',
'Continent' => 'Middle East',
),
106 => array(
'City' => 'Dubai',
'Country' => 'United Arab Emirates',
'Continent' => 'Middle East',
),
107 => array(
'City' => 'Montreal',
'Country' => 'Canada',
'Continent' => 'North America',
)
);
$newArray = array();
foreach ($array as $row)
{
$newArray[$row['Continent']][$row['Country']][] = $row['City'];
}
print_r($newArray);
I got idea from Keval's answer. After doing a small change, it is working like charm.
$new_data = array();
foreach ($array as $row)
{
for($i=0; $i<count($row); $i++){
$newArray = array();
$newArray[$i] = $row[$i];
$new_data[] = $newArray;
}
}
print_r($new_data);
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);