I'm afraid that it is simply not possible what I'm trying to do, but I hope you can help me to find a nice way to solve this problem.
I've got the following PHP-array:
Array (
[0] => Array ( [article] => 10.499-1 [operation] => KN_KABEL [date] => 31-05-2013 [hours] => 0 [quantity] => 1 )
[1] => Array ( [article] => 10.499-1 [operation] => LAS_LABEL [date] => 31-05-2013 [hours] => 0 [quantity] => 1 )
[2] => Array ( [article] => 10.499-1 [operation] => ASS_HARNES [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[3] => Array ( [article] => 10.499-1 [operation] => CONTROLE [date] => 07-06-2013 [hours] => 0 [quantity] => 1 )
[4] => Array ( [article] => 24.030 [operation] => LAS_LABEL [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[5] => Array ( [article] => 24.030 [operation] => ZAGEN-RAIL [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[6] => Array ( [article] => 24.030 [operation] => KN_KABEL [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[7] => Array ( [article] => 24.030 [operation] => ASS_RAIL [date] => 05-06-2013 [hours] => 0 [quantity] => 1 )
[8] => Array ( [article] => 791 070-6/GS/P [operation] => GS_UNIT [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[9] => Array ( [article] => 791 070-6/GS/P [operation] => PR_UNIT [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[10] => Array ( [article] => 791 070-6/GS/P [operation] => LAS_LABEL [date] => 04-06-2013 [hours] => 0 [quantity] => 1 )
[11] => Array ( [article] => ESS-0834E/LE-CD200 [operation] => MAGAZIJN [date] => 10-06-2013 [hours] => 0 [quantity] => 1 )
[12] => Array ( [article] => ESS-0834E/LE-CD200 [operation] => PR_UNIT [date] => 11-06-2013 [hours] => 0 [quantity] => 1 )
[13] => Array ( [article] => ESS-0834E/LE-CD200 [operation] => LAB_PLAKKE [date] => 11-06-2013 [hours] => 0 [quantity] => 1 )
)
What I'm trying to do is to count for each date (key "datum") the sum of hours (in this example they are all 0 but I still want to do this because this will change in the future). What would be very practicle is to do a query like sql like SELECT SUM(hours) GROUP BY date but this is no SQL unfortunately.
Is there a way to group (and order) my array by a specific key (in this case "date") or, if not, is there an other way to get the result what I want?
EDIT
I recently added a key "department" which should be grouped by to. Thereby I do not only want to count the sum of "hours", but "quantity" too
Just create simply foreach for this.
$arr = array();
$sums = array();
foreach($arr as $k=>$v)
{
if(!isset($sums[$v['date']][$v['department']]['hours'])) $sums[$v['date']][$v['department']]['hours'] = 0;
if(!isset($sums[$v['date']][$v['department']]['quantity'])) $sums[$v['date']][$v['department']]['quantity'] = 0;
$sums[$v['date']][$v['department']]['hours'] += $v['hours'];
$sums[$v['date']][$v['department']]['quantity'] += $v['quantity'];
}
print_r($sums);
it will create array $sum where keys are your dates. If value doesn't exists it will add the value of hours to 0, else if it exists it will add to existing value.
edit
Fitted to OP needs.
Related
This question already has answers here:
Get Order items and WC_Order_Item_Product in WooCommerce 3
(2 answers)
Closed 5 years ago.
I have woocommrece order detail array which had protective element so when like normal array you can't access them via their key .This array is result of the plugin file code which i have purchased.
I have customize this code so many time but the protective array do not let me access into it. Any idea
Array
(
[1251] => WC_Order_Item_Product Object
(
[extra_data:protected] => Array
(
[product_id] => 0
[variation_id] => 0
[quantity] => 1
[tax_class] =>
[subtotal] => 0
[subtotal_tax] => 0
[total] => 0
[total_tax] => 0
[taxes] => Array
(
[subtotal] => Array
(
)
[total] => Array
(
)
)
)
[data:protected] => Array
(
[order_id] => 2040
[name] => Vanilla Pastries
[product_id] => 776
[variation_id] => 0
[quantity] => 5
[tax_class] => gst_18
[subtotal] => 296.6
[subtotal_tax] => 53.388
[total] => 296.6
[total_tax] => 53.388
[taxes] => Array
(
[total] => Array
(
[5] => 26.694
[10] => 26.694
)
[subtotal] => Array
(
[5] => 26.694
[10] => 26.694
)
)
)
[cache_group:protected] => order-items
[meta_type:protected] => order_item
[object_type:protected] => order_item
[id:protected] => 1251
[changes:protected] => Array
(
)
[object_read:protected] => 1
[default_data:protected] => Array
(
[order_id] => 0
[name] =>
[product_id] => 0
[variation_id] => 0
[quantity] => 1
[tax_class] =>
[subtotal] => 0
[subtotal_tax] => 0
[total] => 0
[total_tax] => 0
[taxes] => Array
(
[subtotal] => Array
(
)
[total] => Array
(
)
)
)
[data_store:protected] => WC_Data_Store Object
(
[instance:WC_Data_Store:private] => WC_Order_Item_Product_Data_Store Object
(
[internal_meta_keys:protected] => Array
(
[0] => _order_id
[1] => _name
[2] => _product_id
[3] => _variation_id
[4] => _quantity
[5] => _tax_class
[6] => _subtotal
[7] => _subtotal_tax
[8] => _total
[9] => _total_tax
[10] => _taxes
[11] => _product_id
[12] => _variation_id
[13] => _qty
[14] => _tax_class
[15] => _line_subtotal
[16] => _line_subtotal_tax
[17] => _line_total
[18] => _line_tax
[19] => _line_tax_data
)
[meta_type:protected] => order_item
[object_id_field_for_meta:protected] => order_item_id
)
[stores:WC_Data_Store:private] => Array
(
[coupon] => WC_Coupon_Data_Store_CPT
[customer] => WC_Customer_Data_Store
[customer-download] => WC_Customer_Download_Data_Store
[customer-session] => WC_Customer_Data_Store_Session
[order] => WC_Order_Data_Store_CPT
[order-refund] => WC_Order_Refund_Data_Store_CPT
[order-item] => WC_Order_Item_Data_Store
[order-item-coupon] => WC_Order_Item_Coupon_Data_Store
[order-item-fee] => WC_Order_Item_Fee_Data_Store
[order-item-product] => WC_Order_Item_Product_Data_Store
[order-item-shipping] => WC_Order_Item_Shipping_Data_Store
[order-item-tax] => WC_Order_Item_Tax_Data_Store
[payment-token] => WC_Payment_Token_Data_Store
[product] => WC_Product_Data_Store_CPT
[product-grouped] => WC_Product_Grouped_Data_Store_CPT
[product-variable] => WC_Product_Variable_Data_Store_CPT
[product-variation] => WC_Product_Variation_Data_Store_CPT
[shipping-zone] => WC_Shipping_Zone_Data_Store
)
[current_class_name:WC_Data_Store:private] => WC_Order_Item_Product_Data_Store
[object_type:WC_Data_Store:private] => order-item-product
)
[meta_data:protected] => Array
(
)
)
)
The object you want to access has the type WC_Order_Item_Product. Use its public methods to query it for data or tell it to do something.
This is how OOP works. You don't even need to know how the object properties are named.
I guess
global $post;
echo '<pre>';
print_r($post->data);
echo '</pre>';
echo $post->data->order_id;
This is the original main array:
Array
(
[0] => Array
(
[subtotal] => 0.6000
[taxes] => 0.0720
[charged_amount] => 0.6720
[total_discount] => 0.0000
[provinceName] => BC
[store_key] => 1
[store_id] => 5834
[categories] => Array
(
[2] => 0.6000
[4] => 0
[3] => 0
)
)
[1] => Array
(
[subtotal] => 29.8500
[taxes] => 2.3270
[charged_amount] => 20.2370
[total_discount] => 11.9400
[provinceName] => MB
[store_key] => 9
[store_id] => 1022
[categories] => Array
(
[2] => 0
[4] => 29.8500
[3] => 0
)
)
[2] => Array
(
[subtotal] => 0.3000
[taxes] => 0.0390
[charged_amount] => 0.3390
[total_discount] => 0.0000
[provinceName] => NB
[store_key] => 8
[store_id] => 1013
[categories] => Array
(
[2] => 0.3000
[4] => 0
[3] => 0
)
)
[3] => Array
(
[subtotal] => 24.3100
[taxes] => 1.1830
[charged_amount] => 10.2830
[total_discount] => 15.2100
[provinceName] => NL
[store_key] => 4
[store_id] => 3033
[categories] => Array
(
[2] => 24.3100
[4] => 0
[3] => 0
)
)
[4] => Array
(
[subtotal] => 1116.3400
[taxes] => 127.6960
[charged_amount] => 1110.0060
[total_discount] => 134.0300
[provinceName] => ON
[store_key] => 2
[store_id] => 1139
[categories] => Array
(
[2] => 85.7300
[4] => 143.2800
[3] => 887.3300
)
)
[5] => Array
(
[subtotal] => 10.8500
[taxes] => 1.4100
[charged_amount] => 12.2600
[total_discount] => 0.0000
[provinceName] => ON
[store_key] => 5
[store_id] => 1116
[categories] => Array
(
[2] => 10.8500
[4] => 0
[3] => 0
)
)
)
I just need to add the values of the array [categories] with same keys and use it further to print the total, but not getting correct output, can someone help me out to get the desired result:
Desired result
An array with same keys but total of individual array values
Array ( [2] => 0.9000 [4] => 29.8500 [3] => 1.5 )
NOTE: Initial array is dynamic can have n number of key value pair
Thanks
The first thing that you need to do is iterate through the outer array. Then, for each row in the outer array, you and to iterate through each entry in the category element. So this means that we have two foreach loops. Inside the inner foreach, we simply set the value for the current index to be the value of the same index on a 'sum' array (if it doesn't already exist), or increment the value of that index if it already exists in the 'sum' array.
<?php
$sumArray = array();
foreach($outerArray as $row)
{
foreach($row["categories"] as $index => $value)
{
$sumArray[$index] = (isset($sumArray[$index]) ? $sumArray[$index] + $value : $value);
}
}
?>
Demo using your example array
I need help. I want to remove array on array list when there is duplicate value.
I have this array
Array
(
[11] => Array
(
[0] => Array
(
[count] => 2
[price] => 1000
[total] => 2000
)
[1] => Array
(
[count] => 0
[price] => 124
[total] => 0
)
[2] => Array
(
[count] => 0
[price] => 1000
[total] => 0
)
[3] => Array
(
[count] => 0
[price] => 2345
[total] => 0
)
)
[12] => Array
(
[0] => Array
(
[count] => 0
[price] => 1000
[total] => 0
)
[1] => Array
(
[count] => 1
[price] => 124
[total] => 124
)
[2] => Array
(
[count] => 0
[price] => 1000
[total] => 0
)
[3] => Array
(
[count] => 0
[price] => 2345
[total] => 0
)
)
)
As you may notice that there are duplication value on price key. But I only want to remove an array if the the count is equal to 0. But if the duplicate price has no value on count. It only remove one duplicate array. My expected output will be like this.
Array
(
[11] => Array
(
[0] => Array
(
[count] => 2
[price] => 1000
[total] => 2000
)
[1] => Array
(
[count] => 0
[price] => 124
[total] => 0
)
[2] => Array
(
[count] => 0
[price] => 2345
[total] => 0
)
)
[12] => Array
(
[0] => Array
(
[count] => 0
[price] => 1000
[total] => 0
)
[1] => Array
(
[count] => 1
[price] => 124
[total] => 124
)
[2] => Array
(
[count] => 0
[price] => 2345
[total] => 0
)
)
)
Please help thanks.
I have a php variabl $purchase_data which when I did
print_r('purchase_data');
I got the output as
Array
(
[downloads] => Array
(
[0] => Array
(
[id] => 28
[options] => Array
(
)
[quantity] => 1
)
)
[fees] => Array
(
)
[subtotal] => 1
[discount] => 0
[tax] => 0
[price] => 1
[purchase_key] => a8d14e34ba425f9de6afe3ad4809587e
[user_email] => esh#test.com
[date] => 2014-05-11 20:07:22
[user_info] => Array
(
[id] => 1
[email] => eshtest.com
[first_name] => esh
[last_name] =>
[discount] => none
[address] =>
)
[post_data] => Array
(
[edd_email] => esh#test.com
[edd_first] => esh
[edd_last] =>
[edd_phone] => 919995871693
[edd-user-id] => 1
[edd_action] => purchase
[edd-gateway] => sample_gateway
)
[cart_details] => Array
(
[0] => Array
(
[name] => Shirt
[id] => 28
[item_number] => Array
(
[id] => 28
[options] => Array
(
)
[quantity] => 1
)
[item_price] => 1
[quantity] => 1
[discount] => 0
[subtotal] => 1
[tax] => 0
[price] => 1
)
)
[gateway] => sample_gateway
[card_info] => Array
(
[card_name] =>
[card_number] =>
[card_cvc] =>
[card_exp_month] =>
[card_exp_year] =>
[card_address] =>
[card_address_2] =>
[card_city] =>
[card_state] =>
[card_country] =>
[card_zip] =>
)
)
How can i store the value to edd_phone [edd_phone] into variable $mobileNo ?
Sorry for ths kind of a question.But badly need help
If you want to assign the value of [edd_phone] to $mobileNo , use this :
$mobileNo = $purchase_data['post_data']['edd_phone'];
As a side note : Your array is very complexly nested. If I were you, I would avoid such complex arrays.
I would like to take the following array and generate a new array that will group all identical keys with the same ID while getting a SUM of the views and grabbing the last DATE only.
Original Array
Array (
[0] => Array (
[id] => 10
[views] => 276
[date] => 2012-01-30 10:55:00
[total] => N
)
[1] => Array (
[id] => 40
[views] => 287
[date] => 2012-01-27 10:00:29
[total] => Y
)
[2] => Array (
[id] => 40
[views] => 824
[date] => 2012-01-29 14:40:45
[total] => Y
)
[3] => Array (
[id] => 42
[views] => 723
[date] => 2012-01-28 20:15:58
[total] => N
)
[4] => Array (
[id] => 43
[views] => 428
[date] => 2012-01-28 17:14:31
[total] => N
)
[5] => Array (
[id] => 45
[views] => 174
[date] => 2012-01-20 18:01:11
[total] => N
)
)
New Array
Array (
[0] => Array (
[id] => 10
[views] => 276
[date] => 2012-01-30 10:55:00
[total] => N
)
[1] => Array (
[id] => 40
[views] => 1111
[date] => 2012-01-29 14:40:45
[total] => Y
)
[2] => Array (
[id] => 42
[views] => 723
[date] => 2012-01-28 20:15:58
[total] => N
)
[3] => Array (
[id] => 43
[views] => 428
[date] => 2012-01-28 17:14:31
[total] => N
)
[4] => Array (
[id] => 45
[views] => 174
[date] => 2012-01-20 18:01:11
[total] => N
)
)
function last($a, $b) {
if (strtotime($a) > strtotime($b))
return $a;
else
return $b;
}
$new_array = array();
foreach ($original_array as $data) {
if (array_key_exists($data['id'], $new_array)) {
$new_array[$data['id']]['views'] += $data['views'];
$new_array[$data['id']]['date'] = last($new_array[$data['id']]['date'], $data['date']);
} else
$new_array[$data['id']] = $data;
}
$new_array = array_values($new_array);