Retrieve results from multi dimensional array - php

This is my first post so hello there all!
Wondering could I get a bit of help with getting my results out of this md array? Its giving me unexpected results. I have looked for answers but am not sure what question I should be asking..
This is a sample of the relevant part of the array [using var_dump]:
["Attendee"] => array(11)
{
["id"]=> int(148)
["firstname"]=> string(5) "dave"
["lastname"]=> string(6) "davey"`
}
This is my foreach code to get the first and last names for each attendee:
foreach ($the_info_array['body']['Registrations'] as $registrations)
{
foreach ($registrations as $regvalue)
{
echo $regvalue['firstname'].” “.$regvalue['lastname'];
}
echo “<br>”;
}
This prints:
a a2 2 5 5 dave davey
a a2 2 5 5 scott davey
a a2 2 5 5 bill davey
etc
rather than what i want ie:
dave davey
scott davey
bill davey
Where am i going wrong, & what are the extra characters?
Thanks in advance for any kind help the community can give!
EDIT>>
Strangely, if I change the echo to
echo $regvalue['firstname'];
it prints:
a25dave
a25scott
a25bill
and
echo $regvalue['firstname']." ";
prints
a 2 5 dave
a 2 5 scott
a 2 5 bill
Here is the complete array minus several records as I can't seem to post that many characters:
array(3) {
["status"]=> string(2) "OK"
["status_code"]=> int(200)
["body"]=> array(1) {
["Registrations"]=> array(12) {
[0]=> array(15) {
["id"]=> int(148)
["status"]=> string(8) "approved"
["date_of_registration"]=> string(19) "2013-12-04 12:43:31"
["final_price"]=> int(0)
["code"]=> string(23) "529f2373bb7555.08033147"
["url_link"]=> string(0) ""
["is_primary"]=> bool(true)
["is_group_registration"]=> bool(true)
["is_going"]=> bool(true)
["is_checked_in"]=> bool(false)
["Event"]=> array(14) {
["id"]=> int(531) ["code"]=> string(15) "1-5298c3675e610"
["name"]=> string(21) "Recital 2014"
["description"]=> string(721) "The Description is this!"
["status"]=> string(6) "active"
["limit"]=> int(280)
["group_registrations_allowed"]=> bool(true)
["group_registrations_max"]=> int(50)
["active"]=> bool(true)
["member_only"]=> bool(false)
["virtual_url"]=> string(0) ""
["call_in_number"]=> string(0) ""
["phone"]=> string(0) ""
["metadata"]=> array(9) {
["default_payment_status"]=> string(10) "Incomplete"
["venue_id"]=> int(1)
["additional_attendee_reg_info"]=> string(1) "1"
["add_attendee_question_groups"]=> array(1) {
[1]=> string(1) "1" }
["date_submitted"]=> string(10) "29/11/2013"
["event_hashtag"]=> string(0) ""
["event_format"]=> string(0) ""
["event_livestreamed"]=> string(0) ""
[""]=> string(0) "" } }
["Attendee"]=> array(11) {
["id"]=> int(219)
["firstname"]=> string(5) “dave”
[“lastname”]=> string(7) "davis"
["address"]=> string(13) "10 The Street"
["address2"]=> string(0) ""
["city"]=> string(8) "The City"
["state"]=> string(10) "The County"
["country"]=> string(0) ""
["zip"]=> string(6) "BN66YY"
["email"]=> string(17) "mail#webhost.com"
["phone"]=> string(12) "012736654432" }
["Transaction"]=> array(9) {
["id"]=> int(219)
["timestamp"]=> string(19) "2013-12-23 00:26:22"
["total"]=> float(132.6)
["amount_paid"]=> int(0)
["status"]=> string(10) "incomplete"
["details"]=> string(0) ""
["tax_data"]=> string(0) ""
["session_data"]=> string(0) ""
["payment_gateway"]=> string(19) "Credit / Debit Card" }
["Datetime"]=> array(8) {
["id"]=> int(0) ["is_primary"]=> bool(true)
["event_start"]=> string(19) "2014-01-11 00:00:00"
["event_end"]=> string(19) "2014-01-11 00:00:00"
["registration_start"]=> string(19) "2012-08-12 16:00:00"
["registration_end"]=> string(19) "2014-01-10 16:00:00"
["limit"]=> int(280) ["tickets_left"]=> int(274) }
["Price"]=> array(9) {
["id"]=> int(0)
["name"]=> string(0) ""
["amount"]=> int(0)
["description"]=> string(0) ""
["limit"]=> int(9999999)
["remaining"]=> int(999999)
["start_date"]=> NULL
["end_date"]=> NULL
["Pricetype"]=> array(8) {
["id"]=> int(1)
["name"]=> string(10) "Base Price"
["is_member"]=> bool(false)
["is_discount"]=> bool(false)
["is_tax"]=> bool(false)
["is_percent"]=> bool(false)
["is_global"]=> bool(true)
["order"]=> int(0) } } } } } }
// Also, don't know if relevant, but the array comes from a json decode originally

The problem is that your looping over each array inside registrations even though most of these are not an attendee (i.e $regvalue will be an id in the first loop status in the second loop e.t.c.).
Try:
foreach ($the_info_array['body']['Registrations'] as $registrations)
{
echo $registrations['Attendee']['firstname']." ".$registrations['Attendee']['lastname'];
echo "<br>";
}

Related

Keep and get order status history with their time stamps in Woocommerce 3

I want to save the order status history for all orders on woocommerce. e.g If the order status is changed from pending to processing the previous status along with its timestamp should be saved so that user can view their order status history through this page. I have done some research but I couldn't find a way to do this.
I would like to hear the suggestions. The order tracking is done using a custom plugin. I am retrieving the order details using the following $order object
$order = wc_get_order( $order_id );
I am retrieving the billing, shipping details, status and products details using this object. It would be somewhat easier for me to maintain and get the order status history using this object. Following is the object data I get from the above method
object(WC_Order)#3738 (14) { ["status_transition":protected]=> bool(false) ["data":protected]=> array(28) { ["parent_id"]=> int(0) ["status"]=> string(10) "processing" ["currency"]=> string(3) "GBP" ["version"]=> string(5) "3.5.0" ["prices_include_tax"]=> bool(false) ["date_created"]=> object(WC_DateTime)#3745 (4) { ["utc_offset":protected]=> int(0) ["date"]=> string(26) "2018-11-01 15:05:40.000000" ["timezone_type"]=> int(1) ["timezone"]=> string(6) "+00:00" } ["date_modified"]=> object(WC_DateTime)#3742 (4) { ["utc_offset":protected]=> int(0) ["date"]=> string(26) "2018-11-01 16:02:14.000000" ["timezone_type"]=> int(1) ["timezone"]=> string(6) "+00:00" } ["discount_total"]=> string(2) "36" ["discount_tax"]=> string(1) "0" ["shipping_total"]=> string(1) "0" ["shipping_tax"]=> string(1) "0" ["cart_tax"]=> string(1) "0" ["total"]=> string(6) "325.30" ["total_tax"]=> string(1) "0" ["customer_id"]=> int(0) ["order_key"]=> string(22) "wc_order_5bdb16e89ce36" ["billing"]=> array(11) { ["first_name"]=> string(6) "Faizan" ["last_name"]=> string(5) "Zahid" ["company"]=> string(4) "Home" ["address_1"]=> string(6) "456789" ["address_2"]=> string(4) "5678" ["city"]=> string(6) "lahore" ["state"]=> string(2) "PB" ["postcode"]=> string(6) "456789" ["country"]=> string(2) "PK" ["email"]=> string(19) "fzahid001#gmail.com" ["phone"]=> string(0) "" } ["shipping"]=> array(9) { ["first_name"]=> string(6) "Tabish" ["last_name"]=> string(5) "Munir" ["company"]=> string(7) "Frizhub" ["address_1"]=> string(8) "34567890" ["address_2"]=> string(7) "4567890" ["city"]=> string(6) "lahore" ["state"]=> string(2) "PB" ["postcode"]=> string(6) "456789" ["country"]=> string(2) "PK" } ["payment_method"]=> string(5) "other" ["payment_method_title"]=> string(5) "other" ["transaction_id"]=> string(0) "" ["customer_ip_address"]=> string(0) "" ["customer_user_agent"]=> string(0) "" ["created_via"]=> string(5) "admin" ["customer_note"]=> string(0) "" ["date_completed"]=> NULL ["date_paid"]=> object(WC_DateTime)#3790 (4) { ["utc_offset":protected]=> int(0) ["date"]=> string(26) "2018-11-01 15:15:33.000000" ["timezone_type"]=> int(1) ["timezone"]=> string(6) "+00:00" } ["cart_hash"]=> string(0) "" } ["items":protected]=> array(0) { } ["items_to_delete":protected]=> array(0) { } ["cache_group":protected]=> string(6) "orders" ["data_store_name":protected]=> string(5) "order" ["object_type":protected]=> string(5) "order" ["id":protected]=> int(983) ["changes":protected]=> array(0) { } ["object_read":protected]=> bool(true) ["extra_data":protected]=> array(0) { } ["default_data":protected]=> array(28) { ["parent_id"]=> int(0) ["status"]=> string(0) "" ["currency"]=> string(0) "" ["version"]=> string(0) "" ["prices_include_tax"]=> bool(false) ["date_created"]=> NULL ["date_modified"]=> NULL ["discount_total"]=> int(0) ["discount_tax"]=> int(0) ["shipping_total"]=> int(0) ["shipping_tax"]=> int(0) ["cart_tax"]=> int(0) ["total"]=> int(0) ["total_tax"]=> int(0) ["customer_id"]=> int(0) ["order_key"]=> string(0) "" ["billing"]=> array(11) { ["first_name"]=> string(0) "" ["last_name"]=> string(0) "" ["company"]=> string(0) "" ["address_1"]=> string(0) "" ["address_2"]=> string(0) "" ["city"]=> string(0) "" ["state"]=> string(0) "" ["postcode"]=> string(0) "" ["country"]=> string(0) "" ["email"]=> string(0) "" ["phone"]=> string(0) "" } ["shipping"]=> array(9) { ["first_name"]=> string(0) "" ["last_name"]=> string(0) "" ["company"]=> string(0) "" ["address_1"]=> string(0) "" ["address_2"]=> string(0) "" ["city"]=> string(0) "" ["state"]=> string(0) "" ["postcode"]=> string(0) "" ["country"]=> string(0) "" } ["payment_method"]=> string(0) "" ["payment_method_title"]=> string(0) "" ["transaction_id"]=> string(0) "" ["customer_ip_address"]=> string(0) "" ["customer_user_agent"]=> string(0) "" ["created_via"]=> string(0) "" ["customer_note"]=> string(0) "" ["date_completed"]=> NULL ["date_paid"]=> NULL ["cart_hash"]=> string(0) "" } ["data_store":protected]=> object(WC_Data_Store)#3734 (4) { ["instance":"WC_Data_Store":private]=> object(WC_Order_Data_Store_CPT)#3804 (3) { ["internal_meta_keys":protected]=> array(78) { [0]=> string(10) "_parent_id" [1]=> string(7) "_status" [2]=> string(9) "_currency" [3]=> string(8) "_version" [4]=> string(19) "_prices_include_tax" [5]=> string(13) "_date_created" [6]=> string(14) "_date_modified" [7]=> string(15) "_discount_total" [8]=> string(13) "_discount_tax" [9]=> string(15) "_shipping_total" [10]=> string(13) "_shipping_tax" [11]=> string(9) "_cart_tax" [12]=> string(6) "_total" [13]=> string(10) "_total_tax" [14]=> string(12) "_customer_id" [15]=> string(10) "_order_key" [16]=> string(8) "_billing" [17]=> string(9) "_shipping" [18]=> string(15) "_payment_method" [19]=> string(21) "_payment_method_title" [20]=> string(15) "_transaction_id" [21]=> string(20) "_customer_ip_address" [22]=> string(20) "_customer_user_agent" [23]=> string(12) "_created_via" [24]=> string(14) "_customer_note" [25]=> string(15) "_date_completed" [26]=> string(10) "_date_paid" [27]=> string(10) "_cart_hash" [28]=> string(14) "_customer_user" [29]=> string(10) "_order_key" [30]=> string(15) "_order_currency" [31]=> string(19) "_billing_first_name" [32]=> string(18) "_billing_last_name" [33]=> string(16) "_billing_company" [34]=> string(18) "_billing_address_1" [35]=> string(18) "_billing_address_2" [36]=> string(13) "_billing_city" [37]=> string(14) "_billing_state" [38]=> string(17) "_billing_postcode" [39]=> string(16) "_billing_country" [40]=> string(14) "_billing_email" [41]=> string(14) "_billing_phone" [42]=> string(20) "_shipping_first_name" [43]=> string(19) "_shipping_last_name" [44]=> string(17) "_shipping_company" [45]=> string(19) "_shipping_address_1" [46]=> string(19) "_shipping_address_2" [47]=> string(14) "_shipping_city" [48]=> string(15) "_shipping_state" [49]=> string(18) "_shipping_postcode" [50]=> string(17) "_shipping_country" [51]=> string(15) "_completed_date" [52]=> string(10) "_paid_date" [53]=> string(10) "_edit_lock" [54]=> string(10) "_edit_last" [55]=> string(14) "_cart_discount" [56]=> string(18) "_cart_discount_tax" [57]=> string(15) "_order_shipping" [58]=> string(19) "_order_shipping_tax" [59]=> string(10) "_order_tax" [60]=> string(12) "_order_total" [61]=> string(15) "_payment_method" [62]=> string(21) "_payment_method_title" [63]=> string(15) "_transaction_id" [64]=> string(20) "_customer_ip_address" [65]=> string(20) "_customer_user_agent" [66]=> string(12) "_created_via" [67]=> string(14) "_order_version" [68]=> string(19) "_prices_include_tax" [69]=> string(15) "_date_completed" [70]=> string(10) "_date_paid" [71]=> string(15) "_payment_tokens" [72]=> string(22) "_billing_address_index" [73]=> string(23) "_shipping_address_index" [74]=> string(15) "_recorded_sales" [75]=> string(29) "_recorded_coupon_usage_counts" [76]=> string(29) "_download_permissions_granted" [77]=> string(20) "_order_stock_reduced" } ["meta_type":protected]=> string(4) "post" ["object_id_field_for_meta":protected]=> string(0) "" } ["stores":"WC_Data_Store":private]=> array(20) { ["coupon"]=> string(24) "WC_Coupon_Data_Store_CPT" ["customer"]=> string(22) "WC_Customer_Data_Store" ["customer-download"]=> string(31) "WC_Customer_Download_Data_Store" ["customer-download-log"]=> string(35) "WC_Customer_Download_Log_Data_Store" ["customer-session"]=> string(30) "WC_Customer_Data_Store_Session" ["order"]=> string(23) "WC_Order_Data_Store_CPT" ["order-refund"]=> string(30) "WC_Order_Refund_Data_Store_CPT" ["order-item"]=> string(24) "WC_Order_Item_Data_Store" ["order-item-coupon"]=> string(31) "WC_Order_Item_Coupon_Data_Store" ["order-item-fee"]=> string(28) "WC_Order_Item_Fee_Data_Store" ["order-item-product"]=> string(32) "WC_Order_Item_Product_Data_Store" ["order-item-shipping"]=> string(33) "WC_Order_Item_Shipping_Data_Store" ["order-item-tax"]=> string(28) "WC_Order_Item_Tax_Data_Store" ["payment-token"]=> string(27) "WC_Payment_Token_Data_Store" ["product"]=> string(25) "WC_Product_Data_Store_CPT" ["product-grouped"]=> string(33) "WC_Product_Grouped_Data_Store_CPT" ["product-variable"]=> string(34) "WC_Product_Variable_Data_Store_CPT" ["product-variation"]=> string(35) "WC_Product_Variation_Data_Store_CPT" ["shipping-zone"]=> string(27) "WC_Shipping_Zone_Data_Store" ["webhook"]=> string(21) "WC_Webhook_Data_Store" } ["current_class_name":"WC_Data_Store":private]=> string(23) "WC_Order_Data_Store_CPT" ["object_type":"WC_Data_Store":private]=> string(5) "order" } ["meta_data":protected]=> array(5) { [0]=> object(WC_Meta_Data)#3792 (2) { ["current_data":protected]=> array(3) { ["id"]=> int(2671) ["key"]=> string(17) "_vc_post_settings" ["value"]=> array(1) { ["vc_grid_id"]=> array(0) { } } } ["data":protected]=> array(3) { ["id"]=> int(2671) ["key"]=> string(17) "_vc_post_settings" ["value"]=> array(1) { ["vc_grid_id"]=> array(0) { } } } } [1]=> object(WC_Meta_Data)#3793 (2) { ["current_data":protected]=> array(3) { ["id"]=> int(2718) ["key"]=> string(14) "slide_template" ["value"]=> string(7) "default" } ["data":protected]=> array(3) { ["id"]=> int(2718) ["key"]=> string(14) "slide_template" ["value"]=> string(7) "default" } } [2]=> object(WC_Meta_Data)#3795 (2) { ["current_data":protected]=> array(3) { ["id"]=> int(2719) ["key"]=> string(19) "_bewpi_invoice_date" ["value"]=> string(19) "2018-11-01 15:08:31" } ["data":protected]=> array(3) { ["id"]=> int(2719) ["key"]=> string(19) "_bewpi_invoice_date" ["value"]=> string(19) "2018-11-01 15:08:31" } } [3]=> object(WC_Meta_Data)#3794 (2) { ["current_data":protected]=> array(3) { ["id"]=> int(2720) ["key"]=> string(21) "_bewpi_invoice_number" ["value"]=> string(3) "983" } ["data":protected]=> array(3) { ["id"]=> int(2720) ["key"]=> string(21) "_bewpi_invoice_number" ["value"]=> string(3) "983" } } [4]=> object(WC_Meta_Data)#3797 (2) { ["current_data":protected]=> array(3) { ["id"]=> int(2721) ["key"]=> string(23) "_bewpi_invoice_pdf_path" ["value"]=> string(19) "2018/00983-2018.pdf" } ["data":protected]=> array(3) { ["id"]=> int(2721) ["key"]=> string(23) "_bewpi_invoice_pdf_path" ["value"]=> string(19) "2018/00983-2018.pdf" } } } }
You can use a custom field to keep the order status history. Then you will use the following hooked functions that will:
start registering pending status on order creation once the order has been placed by customer,
update the history each time the status change.
The code:
// Initiating the history process registering pending status on order creation
add_action( 'woocommerce_checkout_create_order', 'init_order_status_history', 20, 4 );
function init_order_status_history( $order, $data ){
// Set the default time zone (http://php.net/manual/en/timezones.php)
date_default_timezone_set('Europe/Paris');
// Init order status history on order creation.
$order->update_meta_data( '_status_history', array( time() => 'pending' ) );
}
// Getting each status change history and saving the data
add_action( 'woocommerce_order_status_changed', 'order_status_history', 20, 4 );
function order_status_history( $order_id, $old_status, $new_status, $order ){
// Set the default time zone (http://php.net/manual/en/timezones.php)
date_default_timezone_set('Europe/Paris');
// Get order status history
$order_status_history = $order->get_meta( '_status_history' ) ? $order->get_meta( '_status_history' ) : array();
// Add the current timestamp with the new order status to the history array
$order_status_history[time()] = $new_status;
// Update the order status history (as order meta data)
$order->update_meta_data( '_status_history', $order_status_history );
$order->save(); // Save
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
The following shortcode will output the different order statuses and timestamps:
add_shortcode( 'status_history', 'get_order_status_history' );
function get_order_status_history(){
// Get an instance of the WC_Order object from the order ID
$order = wc_get_order( $order_id );
// Get the history data
$status_history = $order->get_meta('_status_history');
$output = '<div class="order-statuses-container">';
// Loop through the statuses (and timestamps)
foreach( $status_history as $timestamp => $order_status ){
output .= '<div class="status-step">' . ucfirst( $order_status ) . '<br>
<span class="date-time">' . date('Y-m-d H:i:s', $timestamp ) . '</span></div>';
}
return $output . '</div>';
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
You still need:
to set the correct date format (easy to set using the date() documentation)
to set the correct time zone in both first functions
to set the correct html structure output in the shortcode function.
to style as desired adding some CSS rules to your theme's styles.css file
USAGE:
1) In the Wordpress text editor of a post or a page: [status_history]
2) In PHP code: echo do_shortcode( "[status_history]" );

How to get the count of each field inside a group tab object

This is my object and I want to get the number of fields in each tab. I'm thinking of skipping the field if its type is a Tab then start counting the fields before the next Type of tab..
Anyway here is my object below:
array(49) {
[0]=>
array(12) {
["key"]=>
string(19) "field_596796ae0c4c4"
["label"]=>
string(7) "GENERAL"
["name"]=>
string(0) ""
["_name"]=>
string(0) ""
["type"]=>
string(3) "tab"
["order_no"]=>
int(0)
["instructions"]=>
string(0) ""
["required"]=>
int(0)
["id"]=>
string(10) "acf-field-"
["class"]=>
string(3) "tab"
["conditional_logic"]=>
array(3) {
["status"]=>
int(0)
["rules"]=>
array(1) {
[0]=>
array(3) {
["field"]=>
string(19) "field_5964e9dd6df85"
["operator"]=>
string(2) "=="
["value"]=>
string(0) ""
}
}
["allorany"]=>
string(3) "all"
}
["field_group"]=>
int(165)
}
[1]=>
array(18) {
["key"]=>
string(19) "field_5964e9206df82"
["label"]=>
string(5) "Brand"
["name"]=>
string(5) "brand"
["_name"]=>
string(5) "brand"
["type"]=>
string(8) "taxonomy"
["order_no"]=>
int(1)
["instructions"]=>
string(0) ""
["required"]=>
int(1)
["id"]=>
string(15) "acf-field-brand"
["class"]=>
string(8) "taxonomy"
["conditional_logic"]=>
array(3) {
["status"]=>
int(0)
["rules"]=>
array(1) {
[0]=>
array(3) {
["field"]=>
string(19) "field_5964e9dd6df85"
["operator"]=>
string(2) "=="
["value"]=>
string(7) "regular"
}
}
["allorany"]=>
string(3) "all"
}
["taxonomy"]=>
string(8) "pa_brand"
["field_type"]=>
string(6) "select"
["allow_null"]=>
int(0)
["load_save_terms"]=>
int(0)
["return_format"]=>
string(2) "id"
["field_group"]=>
int(165)
["multiple"]=>
int(0)
}
[2]=>
array(12) {
["key"]=>
string(19) "field_596796da0c4c5"
["label"]=>
string(6) "LAUNCH"
["name"]=>
string(0) ""
["_name"]=>
string(0) ""
["type"]=>
string(3) "tab"
["order_no"]=>
int(2)
["instructions"]=>
string(0) ""
["required"]=>
int(0)
["id"]=>
string(10) "acf-field-"
["class"]=>
string(3) "tab"
["conditional_logic"]=>
array(3) {
["status"]=>
int(0)
["rules"]=>
array(1) {
[0]=>
array(3) {
["field"]=>
string(19) "field_5964e9dd6df85"
["operator"]=>
string(2) "=="
["value"]=>
string(0) ""
}
}
["allorany"]=>
string(3) "all"
}
["field_group"]=>
int(165)
}
[3]=>
array(15) {
["key"]=>
string(19) "field_5964e99e6df84"
["label"]=>
string(20) "Announced (Globally)"
["name"]=>
string(18) "announced_globally"
["_name"]=>
string(18) "announced_globally"
["type"]=>
string(11) "date_picker"
["order_no"]=>
int(3)
["instructions"]=>
string(23) "Date globally announced"
["required"]=>
int(0)
["id"]=>
string(28) "acf-field-announced_globally"
["class"]=>
string(11) "date_picker"
["conditional_logic"]=>
array(3) {
["status"]=>
int(0)
["rules"]=>
array(1) {
[0]=>
array(3) {
["field"]=>
string(19) "field_5964e9dd6df85"
["operator"]=>
string(2) "=="
["value"]=>
string(7) "regular"
}
}
["allorany"]=>
string(3) "all"
}
["date_format"]=>
string(4) "mmyy"
["display_format"]=>
string(5) "MM yy"
["first_day"]=>
int(1)
["field_group"]=>
int(165)
}
or this object that I simplified and run in Javascript:
var productObject = [
{
"type":"tab",
"value":"GENERAL"
},
{
"type":"taxonomy",
"value":"56",
"label":"Brand"
},
{
"type":"tab",
"value":"LAUNCH"
},
{
"type":"text",
"name":"announced_ph",
"label":"Announced(Philippines)",
"value":"07072017"
},{
"type":"text",
"name":"announced_global",
"label":"Announced(Global)",
"value":"09092017"
},{
"type":"tab",
"value":"NETWORK"
},{
"type":"text",
"name":"sim_type",
"label":"SIM",
"value":"dualnano"
},{
"type":"text",
"name":"broadband_speed",
"label":"Broadband Speed",
"value":"LTE Cat6"
},{
"type":"text",
"name":"broadband_speed",
"label":"Broadband Speed",
"value":"LTE Cat6"
}
];
I need to know the number of fields/objects after an object with a type of "tab". Because I need to know it for the rowspan value of my table.
The ouput I'm thinking should be:
Tab "General" has 1 field
Tab "Launch" has 2 field
Tab "Network" has 3 field
Thanks!
Loop over the object, then just check if the type is equal to tab.
Something like if (productObject.type == 'tab').
If it is equal to tab, set a counter variable to 0, then just increment it until you reach the next tab value.
my_counter = 0
tab_type = ''
for po in productObject:
if po['type'] == 'tab':
my_counter = 0
tab_type = po['value']
my_counter += 1
Something like this.
Using the productObject, I'll based it on the key value
//Getting all "value" keys
var arr = $.map(productObject,function(k,v){
if(k.type == "tab"){
return k.value;
}
});
//Counting each duplicates
var map = arr.reduce(function(prev, cur) {
prev[cur] = (prev[cur] || 0) + 1;
return prev;
}, {});
//Returns JSON
console.log(JSON.stringify(map));
Sample Output:
{"GENERAL":1,"LAUNCH":1,"NETWORK":1}

WooCommerce Booking - Retrieving specific data in Cart?

I have this array, that is dumped from WooCommerce using these lines:
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
// some code here…
}
If I make a raw output of my cart ($items) I get this:
array(1) {
["f584d8671586d336d84e8cf9ed43303c"]=>
array(11) {
["booking"]=>
array(15) {
["_year"]=>
int(2016)
["_month"]=>
int(8)
["_day"]=>
int(28)
["_persons"]=>
array(1) {
[0]=>
int(1)
}
["_date"]=>
string(9) "2016-8-28"
["date"]=>
string(13) "28 août 2016"
["_time"]=>
string(5) "21:30"
["time"]=>
string(11) "21 h 30 min"
["_qty"]=>
int(1)
["Personnes"]=>
int(1)
["_start_date"]=>
int(1472419800)
["_end_date"]=>
int(1472421600)
["_all_day"]=>
int(0)
["_cost"]=>
int(0)
["_booking_id"]=>
int(13013)
}
["product_id"]=>
int(12856)
["variation_id"]=>
int(0)
["variation"]=>
array(0) {
}
["quantity"]=>
int(1)
["line_total"]=>
float(0)
["line_tax"]=>
int(0)
["line_subtotal"]=>
int(0)
["line_subtotal_tax"]=>
int(0)
["line_tax_data"]=>
array(2) {
["total"]=>
array(0) {
}
["subtotal"]=>
array(0) {
}
}
["data"]=>
object(WC_Product_Booking)#11131 (20) {
["availability_rules":"WC_Product_Booking":private]=>
array(0) {
}
["id"]=>
int(12856)
["post"]=>
object(WP_Post)#11132 (24) {
["ID"]=>
int(12856)
["post_author"]=>
string(2) "14"
["post_date"]=>
string(19) "2016-08-16 22:04:09"
["post_date_gmt"]=>
string(19) "2016-08-16 20:04:09"
["post_content"]=>
string(0) ""
["post_title"]=>
string(10) "La Cuchara"
["post_excerpt"]=>
string(0) ""
["post_status"]=>
string(7) "publish"
["comment_status"]=>
string(4) "open"
["ping_status"]=>
string(6) "closed"
["post_password"]=>
string(0) ""
["post_name"]=>
string(12) "la-cuchara-2"
["to_ping"]=>
string(0) ""
["pinged"]=>
string(0) ""
["post_modified"]=>
string(19) "2016-08-16 22:13:52"
["post_modified_gmt"]=>
string(19) "2016-08-16 20:13:52"
["post_content_filtered"]=>
string(0) ""
["post_parent"]=>
int(0)
["guid"]=>
string(59) ""
["menu_order"]=>
int(0)
["post_type"]=>
string(7) "product"
["post_mime_type"]=>
string(0) ""
["comment_count"]=>
string(1) "0"
["filter"]=>
string(3) "raw"
}
["product_type"]=>
string(7) "booking"
["shipping_class":protected]=>
string(0) ""
["shipping_class_id":protected]=>
int(0)
["total_stock"]=>
NULL
["supports":protected]=>
array(0) {
}
["price"]=>
string(1) "0"
["wc_display_cost"]=>
string(0) ""
["wc_booking_base_cost"]=>
string(0) ""
["wc_booking_min_duration"]=>
string(1) "1"
["wc_booking_cost"]=>
string(0) ""
["wc_booking_has_resources"]=>
string(2) "no"
["wc_booking_has_persons"]=>
string(3) "yes"
["wc_booking_has_person_types"]=>
string(2) "no"
["wc_booking_min_persons_group"]=>
string(1) "1"
["tax_status"]=>
string(7) "taxable"
["stock_status"]=>
string(7) "instock"
["manage_stock"]=>
string(2) "no"
}
}
}
I would like to use specific data values in php variables (the booking date, the time and the person):
["date"]=> string(13) "28 août 2016"
["_persons"]=> array(1) { [0]=> int(1) }
["time"]=> string(11) "21 h 30 min"
How can I do to get this specific values from WooCommerce cart object?
Thanks
You could try first this (but I am unsure as I think it can be an object, so may be it will not work):
echo $items[0]['booking']['date'] . '<br>';
echo $items[0]['booking']['_persons'] . '<br>';
echo $items[0]['booking']['time'] . '<br>';
If not working with a foreach loop it will:
$items = WC()->cart->get_cart();
foreach($items as $item) {
$date = $item['booking']['date'];
$person = $item['booking']['_persons'];
$time = $item['booking']['time'];
}
// displaying values for test
echo 'Booking - Date: ' . $date . '/ Person: ' . $person . '/ Time: ' . $time;
One of these will work, for sure…

How to get object like value:item:private from PHP Array

I have the array like this:
} ["items":"Jcart":private]=> array(3) {
[0]=>
string(1) "3"
[1]=>
string(1) "2"
[2]=>
string(7) "ABC-123" }
How to get the "items" values in a php variable?
///////
Te complete object is:
object(Jcart)#1 (8) {
["config"]=>
array(12) {
["jcartPath"]=>
string(6) "jcart/"
["checkoutPath"]=>
string(12) "checkout.php"
["item"]=>
array(6) {
["id"]=>
string(10) "my-item-id"
["name"]=>
string(12) "my-item-name"
["price"]=>
string(13) "my-item-price"
["qty"]=>
string(11) "my-item-qty"
["url"]=>
string(11) "my-item-url"
["add"]=>
string(13) "my-add-button"
}
["paypal"]=>
array(5) {
["id"]=>
string(38) "seller_1282188508_biz#conceptlogic.com"
["https"]=>
bool(true)
["sandbox"]=>
bool(false)
["returnUrl"]=>
string(0) ""
["notifyUrl"]=>
string(0) ""
}
["currencyCode"]=>
string(3) "USD"
["csrfToken"]=>
bool(false)
["text"]=>
array(14) {
["cartTitle"]=>
string(13) "Shopping Cart"
["singleItem"]=>
string(4) "Item"
["multipleItems"]=>
string(5) "Items"
["subtotal"]=>
string(8) "Subtotal"
["update"]=>
string(6) "update"
["checkout"]=>
string(8) "checkout"
["checkoutPaypal"]=>
string(20) "Checkout with PayPal"
["removeLink"]=>
string(6) "remove"
["emptyButton"]=>
string(5) "empty"
["emptyMessage"]=>
string(19) "Your cart is empty!"
["itemAdded"]=>
string(11) "Item added!"
["priceError"]=>
string(21) "Invalid price format!"
["quantityError"]=>
string(38) "Item quantities must be whole numbers!"
["checkoutError"]=>
string(34) "Your order could not be processed!"
}
["button"]=>
array(4) {
["checkout"]=>
string(0) ""
["paypal"]=>
string(0) ""
["update"]=>
string(0) ""
["empty"]=>
string(0) ""
}
["tooltip"]=>
bool(true)
["decimalQtys"]=>
bool(false)
["decimalPlaces"]=>
int(1)
["priceFormat"]=>
array(3) {
["decimals"]=>
int(2)
["dec_point"]=>
string(1) "."
["thousands_sep"]=>
string(1) ","
}
}
["items":"Jcart":private]=>
array(2) {
[0]=>
string(1) "3"
[1]=>
string(1) "2"
}
["names":"Jcart":private]=>
array(2) {
[3]=>
string(12) "Hockey Stick"
[2]=>
string(13) "Baseball Mitt"
}
["prices":"Jcart":private]=>
array(2) {
[3]=>
string(5) "33.25"
[2]=>
string(5) "19.50"
}
["qtys":"Jcart":private]=>
array(2) {
[3]=>
string(1) "1"
[2]=>
string(2) "20"
}
["urls":"Jcart":private]=>
array(2) {
[3]=>
string(15) "http://bing.com"
[2]=>
string(16) "http://yahoo.com"
}
["subtotal":"Jcart":private]=>
float(423.25)
["itemCount":"Jcart":private]=>
int(21)
}
I just need the values in ["items":"Jcart":private]
I assume you got this by casting an object to an array. If that's the case, you shouldn't be doing that! The property is private because you're not supposed to access it directly because it's not a public API. The object should have a public method which you're supposed to call instead, that's the public API. Something like $foo->getItems() or such. Read the documentation and/or source code.

getting data from array within an object [duplicate]

I'm new to PHP and am trying to access the value of "contactId" from the following:
object(stdClass)#2 (4) {
["contacts"]=> array(1) {
[0]=> object(stdClass)#3 (18) {
["email"]=> string(24) "joewilliams#icontact.com"
["firstName"]=> string(3) "Joe"
["lastName"]=> string(8) "Williams"
["prefix"]=> string(3) "Mr."
["suffix"]=> string(3) "Jr."
["fax"]=> string(0) ""
["phone"]=> string(10) "8668039462"
["street"]=> string(21) "2365 Meridian Parkway"
["street2"]=> string(0) ""
["city"]=> string(6) "Durham"
["state"]=> string(2) "NC"
["postalCode"]=> string(5) "27713"
["createDate"]=> string(19) "2013-03-30 21:22:28"
["status"]=> string(6) "normal"
["bounceCount"]=> NULL
["contactId"]=> string(8) "67295434"
}
}
["limit"]=> int(20)
["offset"]=> int(0)
["total"]=> int(1)
}
How can I retrieve the value of contactId?
Many thanks!
These are not arrays, but an object containing an array containing an object. PHP object access syntax is with ->
$variableName->contacts[0]->contactId

Categories