Jump to content

Try to pull values from an object array within and object.


jason3w

Recommended Posts

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

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                  }      ]
     }        ]
}

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"

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.