jason3w Posted June 1, 2012 Share Posted June 1, 2012 Ok, I'm very new to PHP. I will try my best to explain my question. This is a var_dump of a json object. What i am trying to do is access the information in the 'register_sale_products' object. It contains product info like SKU, Price etc of the products in each sale. In the example below i am trying to list the SKU's. object(stdClass)[1] public 'register_sales' => array 0 => object(stdClass)[2] public 'id' => string '7c969b77-6825-bc65-3e04-64f922a570eb' (length=36) public 'register_id' => string '07709f8e-8d90-11e0-8e09-4040f540b50a' (length=36) public 'invoice_number' => string 'BH8464' (length=6) public 'register_sale_products' => array ... public 'totals' => object(stdClass)[6] ... public 'register_sale_payments' => array ... 1 => object(stdClass)[9]....... I managed to get one SKU from the first "Product" object array 'register_sale_products' using this code. $array = array() ; foreach($data->register_sales as $sale){ $array = $sale->register_sale_products ; } foreach($array as $saleprod){ $details = explode(' / ', $saleprod->sku); $result = $details[0]; } echo $result; I really need to get an a result of each object in the array. Any help much appreciated Quote Link to comment https://forums.phpfreaks.com/topic/263477-try-to-pull-values-from-an-object-array-within-and-object/ Share on other sites More sharing options...
jason3w Posted June 1, 2012 Author Share Posted June 1, 2012 If it helps, this is the raw json code. I'm up to about 3 hours trying to work this out. sore head. { "register_sales" : [ { "id": "7c969b77-6825-bc65-3e04-64f922a570eb", "register_id": "07709f8e-8d90-11e0-8e09-4040f540b50a", "market_id": "3", "customer_id": "ee7709a6-8cfd-11e0-8e09-4040f540b50a", "customer_name": "", "customer": { "id": "ee7709a6-8cfd-11e0-8e09-4040f540b50a", "name": "", "customer_code": "WALKIN", "customer_group_id": "ee76469c-8cfd-11e0-8e09-4040f540b50a", "customer_group_name": "All Customers", "updated_at": "2012-06-01 08:14:16", "deleted_at": "", "balance": "4.890", "year_to_date": "100000.00000", "points": "0", "date_of_birth": "", "sex": "", "custom_field_1": "", "custom_field_2": "", "custom_field_3": "", "custom_field_4": "", "contact": { } }, "user_id": "3fe50420-1947-11e1-a22b-4040dde94e2e", "user_name": "Chris", "sale_date": "2012-06-01 08:14:23", "created_at": "2012-06-01 08:14:16", "updated_at": "2012-06-01 08:14:16", "total_price": 7.27, "total_tax": 0.73, "tax_name": "GST", "note": "", "status": "CLOSED", "short_code": "ty6l4", "invoice_number": "BH8464", "register_sale_products": [ { "id" :"bd37425f-af79-b661-dc8a-25740bb1364c", "product_id" :"3e45f11a-2dc1-11e1-8cd4-4040dde94e2e", "register_id" :"07709f8e-8d90-11e0-8e09-4040f540b50a", "sequence" :"0", "handle" :"toneguirostick", "sku" :"1878", "name" :"Tone Guiro Stick", "quantity" : 1, "price" : 7.27, "price_set" : 0, "tax" : 0.73, "tax_id" : "fbdf1b16-a20f-11e0-8317-4040f540b50a", "tax_rate" : 0.1, "tax_total" : 0.73, "price_total" : 7.27, "display_retail_price_tax_inclusive" : "1", "status" : "CONFIRMED" } ], "totals": { "total_tax" : 0.73, "total_price" : 7.27, "total_payment" : 8, "total_to_pay" : 0 }, "register_sale_payments" : [ { "id" : "2200fb85-445a-d691-483e-1068dc2cafb6", "payment_type_id" : "1", "register_id" : "", "retailer_payment_type_id" : "ee9d3ec8-8cfd-11e0-8e09-4040f540b50a", "name" : "Cash", "label" : "Cash", "payment_date" : "2012-06-01 08:13:42", "amount" : -12 }, { "id" : "c2150b80-29a1-93e7-3c12-7eff199e1b84", "payment_type_id" : "1", "register_id" : "", "retailer_payment_type_id" : "ee9d3ec8-8cfd-11e0-8e09-4040f540b50a", "name" : "Cash", "label" : "Cash", "payment_date" : "2012-06-01 08:13:41", "amount" : 20 } ] } ] } Quote Link to comment https://forums.phpfreaks.com/topic/263477-try-to-pull-values-from-an-object-array-within-and-object/#findComment-1350272 Share on other sites More sharing options...
trq Posted June 1, 2012 Share Posted June 1, 2012 Firstly, just to clarify, that is not a Json object. Json is Javascript, that is a PHP object (it may have been created from Json). Now, as for your question. What the hell is a sku and where exactly in your post can we see it? Quote Link to comment https://forums.phpfreaks.com/topic/263477-try-to-pull-values-from-an-object-array-within-and-object/#findComment-1350273 Share on other sites More sharing options...
jason3w Posted June 1, 2012 Author Share Posted June 1, 2012 Hey Thorpe, The SKU is basically just one piece of information about the product. The "register_sale_products" is an object inside the main object, and it contains the values i need to get to. See snippet below. I'm noob terms i'd like to just go echo $data->register_sales->register_sale_products->sku; (i know i can't do that, but that is basically the path to the info i want to work with) $data is just the json_decoded object. "register_sale_products": [ { "id" :"bd37425f-af79-b661-dc8a-25740bb1364c", "product_id" :"3e45f11a-2dc1-11e1-8cd4-4040dde94e2e", "register_id" :"07709f8e-8d90-11e0-8e09-4040f540b50a", "sequence" :"0", "handle" :"toneguirostick", "sku" :"1878", "name" :"Tone Guiro Stick", "quantity" : 1, "price" : 7.27, "price_set" : 0, "tax" : 0.73, "tax_id" : "fbdf1b16-a20f-11e0-8317-4040f540b50a", "tax_rate" : 0.1, "tax_total" : 0.73, "price_total" : 7.27, "display_retail_price_tax_inclusive" : "1", "status" : "CONFIRMED" Quote Link to comment https://forums.phpfreaks.com/topic/263477-try-to-pull-values-from-an-object-array-within-and-object/#findComment-1350275 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.