I'm trying to extract all the variables from a complex array of nutrition results from Neutronix API.
The array is as follows:
Food Array:
Array ( [foods] =>
Array ( [0] => Array (
[food_name] => kale
[brand_name] =>
[serving_qty] => 1
[serving_unit] => cup, chopped
[serving_weight_grams] => 130
[nf_calories] => 36.4
[nf_total_fat] => 0.52
[nf_saturated_fat] => 0.07
[nf_cholesterol] => 0
[nf_sodium] => 29.9
[nf_total_carbohydrate] => 7.32
[nf_dietary_fiber] => 2.6
[nf_sugars] => 1.63
[nf_protein] => 2.47
[nf_potassium] => 296.4
[nf_p] => 36.4
[full_nutrients] => Array (
[0] => Array (
[attr_id] => 203
[value] => 2.47
)
[1] => Array (
[attr_id] => 204
[value] => 0.52
)
[2] => Array (
[attr_id] => 205
[value] => 7.319
)
[3] => Array (
[attr_id] => 207
[value] => 1.131
)
[4] => Array (
[attr_id] => 208
[value] => 36.4
)
[5] => Array (
[attr_id] => 221
[value] => 0
)
[6] => Array (
[attr_id] => 255
[value] => 118.56
)
[7] => Array (
[attr_id] => 262
[value] => 0
)
[8] => Array (
[attr_id] => 263
[value] => 0
)
[9] => Array (
[attr_id] => 268
[value] => 152.1
)
[10] => Array (
[attr_id] => 269
[value] => 1.625
)
[11] => Array (
[attr_id] => 291
[value] => 2.6
)
[12] => Array (
[attr_id] => 301
[value] => 93.6
)
[13] => Array (
[attr_id] => 303
[value] => 1.17
)
[14] => Array (
[attr_id] => 304
[value] => 23.4
)
[15] => Array (
[attr_id] => 305
[value] => 36.4
)
[16] => Array (
[attr_id] => 306
[value] => 296.4
)
[17] => Array (
[attr_id] => 307
[value] => 29.9
)
[18] => Array (
[attr_id] => 309
[value] => 0.312
)
[19] => Array (
[attr_id] => 312
[value] => 0.2028
)
[20] => Array (
[attr_id] => 315
[value] => 0.5408
)
[21] => Array (
[attr_id] => 317
[value] => 1.17
)
)
[nix_brand_name] =>
[nix_brand_id] =>
[nix_item_name] =>
[nix_item_id] =>
[upc] =>
[consumed_at] => 2017-09-08T22:44:54+00:00
[metadata] => Array ( )
[source] => 1
[ndb_no] => 11234
[tags] => Array (
[item] => kale
[measure] =>
[quantity] => 1.0
[tag_id] => 644
)
[alt_measures] => Array (
[0] => Array (
[serving_weight] => 130
[measure] => cup, chopped
[seq] => 1
[qty] => 1 )
[1] => Array (
[serving_weight] => 130
[measure] => cup
[seq] => 80
[qty] => 1 )
[2] => Array (
[serving_weight] => 2.71
[measure] => tsp
[seq] => 101
[qty] => 1 )
[3] => Array (
[serving_weight] => 8.13
[measure] => tbsp
[seq] => 102
[qty] => 1 )
)
[lat] =>
[lng] =>
[meal_type] => 5
[photo] => Array (
[thumb] => https://d2xdmhkmkbyw75.cloudfront.net/644_thumb.jpg
[highres] => https://d2xdmhkmkbyw75.cloudfront.net/644_highres.jpg
)
[sub_recipe] =>
)
)
)
My PHP Code so far is below. I've inserted comments where I am stuck as to how to get the variables.
I'm also not sure if I'm looping the segments of the array correctly.
foreach ($foods as $food){
foreach($food as $key => $val) {
//get values for each variable
foreach($full_nutrients as $nutrient){
foreach($nutrient as $key => $val){
//get values for each variable
}
}
foreach($metadata as $meta){
$source = $meta['source'];
$ndb_no = $meta['ndb_no'];
foreach($tags as $tag){
$tag_item = $tag['item'];
$tag_measure = $tag['measure'];
$tag_quantity = $tag['quantity'];
$tag_id = $tag['tag_id'];
}
}
foreach($alt_measures as $alt_meaasure){
foreach($alt_meaasure as $key => $val){
//get alt_measures
}
}
foreach($photo as $image){
$image_thumb = $image['thumb'];
$image_highres = $image['highres'];
}
}
}
Related
I want to do a cumulative sum of data from MySQL query. Already my SQL query working well but i don't know how to do a cumulative sum of column 'suma'. I want to stay with dates from MySQL query table but it should look like this that next index will have cumulative sum of this index and previous.
My array from MySQL query look like this.
[0] => Array
(
[data] => 2017-11-01
[SUMA] => 19
)
[1] => Array
(
[data] => 2017-11-02
[SUMA] => 97
)
[2] => Array
(
[data] => 2017-11-03
[SUMA] => 296
)
[3] => Array
(
[data] => 2017-11-05
[SUMA] => 58
)
[4] => Array
(
[data] => 2017-11-06
[SUMA] => 216
)
[5] => Array
(
[data] => 2017-11-07
[SUMA] => 194
)
[6] => Array
(
[data] => 2017-11-08
[SUMA] => 444
)
[7] => Array
(
[data] => 2017-11-09
[SUMA] => 301
)
[8] => Array
(
[data] => 2017-11-10
[SUMA] => 213
)
[9] => Array
(
[data] => 2017-11-11
[SUMA] => 5
)
[10] => Array
(
[data] => 2017-11-12
[SUMA] => 8
)
[11] => Array
(
[data] => 2017-11-13
[SUMA] => 186
)
[12] => Array
(
[data] => 2017-11-14
[SUMA] => 227
)
[13] => Array
(
[data] => 2017-11-15
[SUMA] => 180
)
[14] => Array
(
[data] => 2017-11-16
[SUMA] => 225
)
)
And I want to this array or new one look like this:
[0] => Array
(
[data] => 2017-11-01
[SUMA] => 19
)
[1] => Array
(
[data] => 2017-11-02
[SUMA] => 116
)
[2] => Array
(
[data] => 2017-11-03
[SUMA] => 412
)
[3] => Array
(
[data] => 2017-11-05
[SUMA] => 470
)
[4] => Array
(
[data] => 2017-11-06
[SUMA] => 686
)
[5] => Array
(
[data] => 2017-11-07
[SUMA] => 880
)
[6] => Array
(
[data] => 2017-11-08
[SUMA] => 1324
)
[7] => Array
(
[data] => 2017-11-09
[SUMA] => 1625
)
[8] => Array
(
[data] => 2017-11-10
[SUMA] => 1838
)
[9] => Array
(
[data] => 2017-11-11
[SUMA] => 1843
)
[10] => Array
(
[data] => 2017-11-12
[SUMA] => 1851
)
[11] => Array
(
[data] => 2017-11-13
[SUMA] => 2037
)
[12] => Array
(
[data] => 2017-11-14
[SUMA] => 2264
)
[13] => Array
(
[data] => 2017-11-15
[SUMA] => 2444
)
[14] => Array
(
[data] => 2017-11-16
[SUMA] => 2699
)
I tried like this but it's only showing me last date and full cumulative instead of each index.
while ($row = mysqli_fetch_assoc($result)) {
$myArray['data'] = $row['data'];
$myArray['suma'] += $row['SUMA'];
}
Do it while creating array
$s=0;
$myArray = [];
while ($row = mysqli_fetch_assoc($result)) {
$temp['data'] = $row['data'];
$s = $temp['suma'] = $row['SUMA'] + $s;
$myArray[] = $temp;
}
echo '<pre>';
print_r($myArray);
<?php
$array=array(
0 => Array
(
'data' => '2017-11-01',
'SUMA' => 19
),
1 => Array
(
'data' => '2017-11-02',
'SUMA' => 97
),
2 => Array
(
'data' => '2017-11-03',
'SUMA' => 296,
),
3 => Array
(
'data' => '2017-11-05',
'SUMA' => 58,
),
);
$i=0;
while ($i<count($array) )
{
if($i==0) {
$newArray[0]['data'] = $array[$i]['data'];
$newArray[0]['SUMA'] = $array[$i]['SUMA'];
}
else{
$newArray[$i]['data']=$array[$i]['data'];
$newArray[$i]['SUMA']=$array[$i]['SUMA']+$newArray[$i-1]['SUMA'];
}
$i++;
}
echo '<pre>';
print_r($newArray);
and the output is :
Array
(
[0] => Array
(
[data] => 2017-11-01
[SUMA] => 19
)
[1] => Array
(
[data] => 2017-11-02
[SUMA] => 116
)
[2] => Array
(
[data] => 2017-11-03
[SUMA] => 412
)
[3] => Array
(
[data] => 2017-11-05
[SUMA] => 470
)
)
You can remove the if/else and keep only the else but you get a notice for position -1 of the array. The code is working fine you just have to disable the notice warning (i don't recommend it do it only if you are 100% sure)
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;
I have an Multi Dimensional array like below.
<?php$array = Array ( [ResultIndex] => OB4 [Source] => 3 [IsLCC] => 1 [IsRefundable] => [GSTAllowed] => [AirlineRemark] => this is a test from aditya [Fare] => Array ( [Currency] => INR [BaseFare] => 865 [Tax] => 613 [TaxBreakup] => Array ( [0] => Array ( [key] => YQTax [value] => 0 ) [1] => Array ( [key] => YR [value] => 0 ) [2] => Array ( [key] => PSF [value] => 0 ) [3] => Array ( [key] => UDF [value] => 0 ) [4] => Array ( [key] => JNTax [value] => 52 ) [5] => Array ( [key] => INTax [value] => 0 ) [6] => Array ( [key] => TransactionFee [value] => 0 ) [7] => Array ( [key] => OtherTaxes [value] => 0 ) ) [YQTax] => 0 [AdditionalTxnFeeOfrd] => 0 [AdditionalTxnFeePub] => 0 [PGCharge] => 0 [OtherCharges] => 0 [ChargeBU] => Array ( [0] => Array ( [key] => TBOMARKUP [value] => 0 ) [1] => Array ( [key] => CONVENIENCECHARGE [value] => 0 ) [2] => Array ( [key] => OTHERCHARGE [value] => 0 ) ) [Discount] => 0 [PublishedFare] => 1478 [CommissionEarned] => 22.06 [PLBEarned] => 14.09 [IncentiveEarned] => 14.33 [OfferedFare] => 1427.52 [TdsOnCommission] => 4.41 [TdsOnPLB] => 2.82 [TdsOnIncentive] => 2.87 [ServiceFee] => 0 [TotalBaggageCharges] => 0 [TotalMealCharges] => 0 [TotalSeatCharges] => 0 [TotalSpecialServiceCharges] => 0 ) [FareBreakdown] => Array ( [0] => Array ( [Currency] => INR [PassengerType] => 1 [PassengerCount] => 1 [BaseFare] => 865 [Tax] => 613 [YQTax] => 0 [AdditionalTxnFeeOfrd] => 0 [AdditionalTxnFeePub] => 0 [PGCharge] => 0 ) ) [Segments] => Array ( [0] => Array ( [0] => Array ( [Baggage] => 10 Kg [CabinBaggage] => 7 Kg [TripIndicator] => 1 [SegmentIndicator] => 1 [Airline] => Array ( [AirlineCode] => SG [AirlineName] => SpiceJet [FlightNumber] => 1033 [FareClass] => C [OperatingCarrier] => ) [NoOfSeatAvailable] => 4 [Origin] => Array ( [Airport] => Array ( [AirportCode] => BLR [AirportName] => Hindustan [Terminal] => [CityCode] => BLR [CityName] => Bangalore [CountryCode] => IN [CountryName] => India ) [DepTime] => 2017-10-21T17:40:00 ) [Destination] => Array ( [Airport] => Array ( [AirportCode] => MAA [AirportName] => Chennai [Terminal] => [CityCode] => MAA [CityName] => Chennai [CountryCode] => IN [CountryName] => India ) [ArrTime] => 2017-10-21T18:40:00 ) [Duration] => 60 [GroundTime] => 0 [Mile] => 0 [StopOver] => [StopPoint] => [StopPointArrivalTime] => 2017-10-21T18:40:00 [StopPointDepartureTime] => 2017-10-21T17:40:00 [Craft] => DH8 [Remark] => [IsETicketEligible] => 1 [FlightStatus] => Confirmed [Status] => ) ) ) [LastTicketDate] => [TicketAdvisory] => [FareRules] => Array ( [0] => Array ( [Origin] => BLR [Destination] => MAA [Airline] => SG [FareBasisCode] => CSAVER [FareRuleDetail] => [FareRestriction] => ) ) [AirlineCode] => SG [ValidatingAirline] => SG );?>
how to make it print using foreach loop in php. if any one have idea to rectify this please suggest me
Hi guys i have this array when i do print_r($p)
Array
(
[0] => Array
(
[product] => Array
(
[title] => test
[id] => 9
[created_at] => 2015-08-11 19:32:05
[isNew] =>
[type] => simple
[status] => publish
[price] => 10.00
[regular_price] => 10.00
[sale_price] => 6.00
[stock_quantity] => 19999985
[featured] => 1
[on_sale] =>
[description] =>
[short_description] =>
[categories] => Array
(
)
[tags] => Array
(
)
[images] => Array
(
[0] => Array
(
[src] =>
)
)
[featured_src] =>
[attributes] => Array
(
)
[variations] =>
)
)
[1] => Array
(
[product] => Array
(
[title] => test222222
[id] => 97
[created_at] => 2015-08-31 17:40:54
[isNew] =>
[type] => variation
[status] => publish
[price] => 1
[regular_price] => 2
[sale_price] => 1
[stock_quantity] => 1999974
[featured] => 1
[on_sale] => 1
[description] => <p>tasdasd</p>
[short_description] =>
[categories] => Array
(
)
[tags] => Array
(
)
[images] => Array
(
[0] => Array
(
[src] =>
)
)
[featured_src] =>
[attributes] => Array
(
[0] => Array
(
[name] => Color
[slug] => Color
[position] => 0
[visible] => 1
[variation] => 1
[options] => Array
(
[0] => black
[1] => White
)
)
)
[variations] => Array
(
[0] => Array
(
[id] => 98
[price] => 1
[regular_price] => 2
[stock] => 199969
[color] => black
)
[1] => Array
(
[id] => 97
[price] => 1
[regular_price] => 2
[stock] => 1999974
[color] => White
)
)
)
)
[2] => Array
(
[product] => Array
(
[title] => test222222
[id] => 98
[created_at] => 2015-08-31 17:40:54
[isNew] =>
[type] => variation
[status] => publish
[price] => 1
[regular_price] => 2
[sale_price] => 1
[stock_quantity] => 199969
[featured] => 1
[on_sale] => 1
[description] => <p>tasdasd</p>
[short_description] =>
[categories] => Array
(
)
[tags] => Array
(
)
[images] => Array
(
[0] => Array
(
[src] =>
)
)
[featured_src] =>
[attributes] => Array
(
[0] => Array
(
[name] => Color
[slug] => Color
[position] => 0
[visible] => 1
[variation] => 1
[options] => Array
(
[0] => black
[1] => White
)
)
)
[variations] => Array
(
[0] => Array
(
[id] => 98
[price] => 1
[regular_price] => 2
[stock] => 199969
[color] => black
)
[1] => Array
(
[id] => 97
[price] => 1
[regular_price] => 2
[stock] => 1999974
[color] => White
)
)
)
)
[3] => Array
(
[product] => Array
(
[title] => test222222
[id] => 76
[created_at] => 2015-08-31 17:40:54
[isNew] =>
[type] => variable
[status] => publish
[price] => 0.00
[regular_price] => 0.00
[sale_price] => 0.00
[stock_quantity] => 50000
[featured] => 1
[on_sale] => 1
[description] => <p>tasdasd</p>
[short_description] =>
[categories] => Array
(
)
[tags] => Array
(
)
[images] => Array
(
[0] => Array
(
[src] => https://localhost/Leminiscate/wp-content/uploads/2015/08/lemniscate_by_eon_brush-d7y8np7-e1441070793605.jpg
)
)
[featured_src] => https://localhost/Leminiscate/wp-content/uploads/2015/08/lemniscate_by_eon_brush-d7y8np7-e1441070793605.jpg
[attributes] => Array
(
[0] => Array
(
[name] => Color
[slug] => Color
[position] => 0
[visible] => 1
[variation] => 1
[options] => Array
(
[0] => black
[1] => White
)
)
)
[variations] => Array
(
[0] => Array
(
[id] => 98
[price] => 1
[regular_price] => 2
[stock] => 199969
[color] => black
)
[1] => Array
(
[id] => 97
[price] => 1
[regular_price] => 2
[stock] => 1999974
[color] => White
)
)
)
)
)
null
i get this with this function
public function test(){
global $wpdb;
global $Pproduct;
global $woocommerce;
$productIds = 9_97_98_76;
$pId = explode("_", $productIds);
foreach($pId as $productID){
$product[] = $Pproduct->get_product($productID, $fields);
$p = $product;
}
print_r($p);
how do i do a foreach loop again to get variation attributes? in given product id 9_97_98_76, product id 97 & 98 are variation products of 76.
I want to get the title of the product and variable attributes, how do i code foreach so that the result returns as the following array : test_test222222 white_test222222 black_test222222 ???
try this.. Hope you are getting the product object for the productID using the get_product() function. Then try array_push to insert all filtered product objects in 1 array.
$product = Array();
foreach($pId as $productID){
array_push($product, $Pproduct->get_product($productID, $fields));
}
now try the following
foreach($product as $temp) {
echo $temp[variations];
}
Try this will get your title
$pId = explode("_", $products);
foreach($pId as $id){
$product = $Pproduct->get_product($id, $fields);
$title = $product['product']['title'];
echo $title."</br>";
}
[5] => Array
(
[id] => 29372
[product_id] => Array
(
[0] => stdClass Object
(
[id] => 1469
[type_id] => 1
[title] => Hearth 2 Hearth
[cover] => 21cf9d7d09d403251ba5d01ff33cd089.jpg
[coverid] => 1178
[inserted_by] => 0
[inserted_date] => 2011-02-11 13:55:23
[status_id] => 0
)
)
[variable_id] => 9
[variable_value] => 2011-02-11
[master_value] =>
[released_date] => 2011-02-11
[price] => Array
(
[0] => Array
(
[product_id] => 1469
[media_format] => VCD
[price] => 29000
[discount] => 5
)
)
[media_format] => VCD
)
[6] => Array
(
[id] => 30074
[product_id] => Array
(
[0] => stdClass Object
(
[id] => 1470
[type_id] => 1
[title] => Hearth 2 Hearth
[cover] => 149ddd4d1d5e567c1300d4831323e1c5.jpg
[coverid] => 1177
[inserted_by] => 6
[inserted_date] => 2011-02-16 15:18:58
[status_id] => 0
)
)
[variable_id] => 9
[variable_value] => 2011-02-11
[master_value] =>
[released_date] => 2011-02-11
[price] => Array
(
[0] => Array
(
[product_id] => 1470
[media_format] => DVD
[price] => 39000
[discount] => 0
)
)
[media_format] => DVD
)
I want the result by media_format = DVD so the whole array is gone, is there anyway to delete an array or remove it?
The same way you "delete" any variable:
unset($array[$index]);