Stack Exchange
PHP get object from array
I have some problems with Shopify integration, and I always have this problems with getting info from objects.

object(Shopify\Object\Product)#32 (3) {
  ["changedFields":"Shopify\Object\AbstractObject":private]=> array(16)
  { ["id"]=> int(575833276481) ["title"]=> string(12) "Test Product"
  ["body_html"]=> NULL ["vendor"]=> string(7) "Printer"
  ["product_type"]=> string(4) "Test" ["created_at"]=>
  object(DateTime)#33 (3) { ["date"]=> string(26) "2018-03-27
  11:01:14.000000" ["timezone_type"]=> int(1) ["timezone"]=> string(6)
  "+03:00" } ["handle"]=> string(15) "test-product-24" ["updated_at"]=>
  object(DateTime)#34 (3) { ["date"]=> string(26) "2018-03-27
  11:01:14.000000" ["timezone_type"]=> int(1) ["timezone"]=> string(6)
  "+03:00" } ["published_at"]=> object(DateTime)#64 (3) { ["date"]=>
  string(26) "2018-03-27 11:01:14.000000" ["timezone_type"]=> int(1)
  ["timezone"]=> string(6) "+03:00" } ["template_suffix"]=> NULL
  ["published_scope"]=> string(6) "global" ["tags"]=> string(14) "Barnes
  & Noble" ["variants"]=> array(1) { [0]=>
  object(Shopify\Object\ProductVariant)#65 (3) {
  ["changedFields":"Shopify\Object\AbstractObject":private]=> array(24)
  { ["id"]=> int(7196665282625) ["product_id"]=> int(575833276481)
  ["title"]=> string(13) "Default Title" ["price"]=> string(4) "0.00"
  ["sku"]=> string(0) "" ["position"]=> int(1) ["inventory_policy"]=>
  string(4) "deny" ["compare_at_price"]=> NULL ["fulfillment_service"]=>
  string(6) "manual" ["inventory_management"]=> NULL ["option1"]=>
  string(13) "Default Title" ["option2"]=> NULL ["option3"]=> NULL
  ["created_at"]=> object(DateTime)#59 (3) { ["date"]=> string(26)
  "2018-03-27 11:01:14.000000" ["timezone_type"]=> int(1) ["timezone"]=>
  string(6) "+03:00" } ["updated_at"]=> object(DateTime)#53 (3) {
  ["date"]=> string(26) "2018-03-27 11:01:14.000000" ["timezone_type"]=>
  int(1) ["timezone"]=> string(6) "+03:00" } ["taxable"]=> bool(true)
  ["barcode"]=> NULL ["grams"]=> int(0) ["image_id"]=> NULL
  ["inventory_quantity"]=> int(1) ["weight"]=> string(1) "0"
  ["weight_unit"]=> string(2) "kg" ["old_inventory_quantity"]=> int(1)
  ["requires_shipping"]=> bool(true) } ["data":protected]=> array(26) {
  ["barcode"]=> NULL ["compare_at_price"]=> NULL ["created_at"]=>
  object(DateTime)#59 (3) { ["date"]=> string(26) "2018-03-27
  11:01:14.000000" ["timezone_type"]=> int(1) ["timezone"]=> string(6)
  "+03:00" } ["fulfillment_service"]=> string(6) "manual" ["grams"]=>
  int(0) ["id"]=> int(7196665282625) ["image_id"]=> NULL
  ["inventory_management"]=> NULL ["inventory_policy"]=> string(4)
  "deny" ["old_inventory_quantity"]=> int(1) ["inventory_quantity"]=>
  int(1) ["inventory_quantity_adjustment"]=> NULL ["metafield"]=> NULL
  ["option1"]=> string(13) "Default Title" ["option2"]=> NULL
  ["option3"]=> NULL ["position"]=> int(1) ["price"]=> string(4) "0.00"
  ["product_id"]=> int(575833276481) ["requires_shipping"]=> bool(true)
  ["sku"]=> string(0) "" ["taxable"]=> bool(true) ["title"]=> string(13)
  "Default Title" ["updated_at"]=> object(DateTime)#53 (3) { ["date"]=>
  string(26) "2018-03-27 11:01:14.000000" ["timezone_type"]=> int(1)
  ["timezone"]=> string(6) "+03:00" } ["weight"]=> string(1) "0"
  ["weight_unit"]=> string(2) "kg" } ["types":protected]=> array(26) {
  ["barcode"]=> string(6) "string" ["compare_at_price"]=> string(6)
  "string" ["created_at"]=> string(8) "DateTime"
  ["fulfillment_service"]=> string(6) "string" ["grams"]=> string(7)
  "integer" ["id"]=> string(7) "integer" ["image_id"]=> string(7)
  "integer" ["inventory_management"]=> string(6) "string"
  ["inventory_policy"]=> string(6) "string" ["old_inventory_quantity"]=>
  string(7) "integer" ["inventory_quantity"]=> string(7) "integer"
  ["inventory_quantity_adjustment"]=> string(6) "string" ["metafield"]=>
  string(5) "array" ["option1"]=> string(6) "string" ["option2"]=>
  string(6) "string" ["option3"]=> string(6) "string" ["position"]=>
  string(7) "integer" ["price"]=> string(6) "string" ["product_id"]=>
  string(7) "integer" ["requires_shipping"]=> string(7) "boolean"
  ["sku"]=> string(6) "string" ["taxable"]=> string(7) "boolean"
  ["title"]=> string(6) "string" ["updated_at"]=> string(8) "DateTime"
  ["weight"]=> string(6) "string" ["weight_unit"]=> string(6) "string" }
  } } ["options"]=> array(1) { [0]=>
  object(Shopify\Object\ProductOption)#51 (3) {
  ["changedFields":"Shopify\Object\AbstractObject":private]=> array(5) {
  ["id"]=> int(860003369025) ["product_id"]=> int(575833276481)
  ["name"]=> string(5) "Title" ["position"]=> int(1) ["values"]=>
  array(1) { [0]=> string(13) "Default Title" } } ["data":protected]=>
  array(5) { ["id"]=> int(860003369025) ["product_id"]=>
  int(575833276481) ["name"]=> string(5) "Title" ["position"]=> int(1)
  ["values"]=> array(1) { [0]=> string(13) "Default Title" } }
  ["types":protected]=> array(5) { ["id"]=> string(7) "integer"
  ["product_id"]=> string(7) "integer" ["name"]=> string(6) "string"
  ["position"]=> string(7) "integer" ["values"]=> string(5) "array" } }
  } ["images"]=> array(0) { } ["image"]=> NULL } ["data":protected]=>
  array(18) { ["body_html"]=> NULL ["created_at"]=> object(DateTime)#33
  (3) { ["date"]=> string(26) "2018-03-27 11:01:14.000000"
  ["timezone_type"]=> int(1) ["timezone"]=> string(6) "+03:00" }
  ["handle"]=> string(15) "test-product-24" ["id"]=> int(575833276481)
  ["images"]=> array(0) { } ["options"]=> array(1) { [0]=>
  object(Shopify\Object\ProductOption)#51 (3) {
  ["changedFields":"Shopify\Object\AbstractObject":private]=> array(5) {
  ["id"]=> int(860003369025) ["product_id"]=> int(575833276481)
  ["name"]=> string(5) "Title" ["position"]=> int(1) ["values"]=>
  array(1) { [0]=> string(13) "Default Title" } } ["data":protected]=>
  array(5) { ["id"]=> int(860003369025) ["product_id"]=>
  int(575833276481) ["name"]=> string(5) "Title" ["position"]=> int(1)
  ["values"]=> array(1) { [0]=> string(13) "Default Title" } }
  ["types":protected]=> array(5) { ["id"]=> string(7) "integer"
  ["product_id"]=> string(7) "integer" ["name"]=> string(6) "string"
  ["position"]=> string(7) "integer" ["values"]=> string(5) "array" } }
  } ["product_type"]=> string(4) "Test" ["published_at"]=>
  object(DateTime)#64 (3) { ["date"]=> string(26) "2018-03-27
  11:01:14.000000" ["timezone_type"]=> int(1) ["timezone"]=> string(6)
  "+03:00" } ["published_scope"]=> string(6) "global" ["tags"]=>
  string(14) "Barnes & Noble" ["template_suffix"]=> NULL ["title"]=>
  string(12) "Test Product" ["metafields_global_title_tag"]=> NULL
  ["metafields_global_description_tag"]=> NULL ["updated_at"]=>
  object(DateTime)#34 (3) { ["date"]=> string(26) "2018-03-27
  11:01:14.000000" ["timezone_type"]=> int(1) ["timezone"]=> string(6)
  "+03:00" } ["variants"]=> array(1) { [0]=>
  object(Shopify\Object\ProductVariant)#65 (3) {
  ["changedFields":"Shopify\Object\AbstractObject":private]=> array(24)
  { ["id"]=> int(7196665282625) ["product_id"]=> int(575833276481)
  ["title"]=> string(13) "Default Title" ["price"]=> string(4) "0.00"
  ["sku"]=> string(0) "" ["position"]=> int(1) ["inventory_policy"]=>
  string(4) "deny" ["compare_at_price"]=> NULL ["fulfillment_service"]=>
  string(6) "manual" ["inventory_management"]=> NULL ["option1"]=>
  string(13) "Default Title" ["option2"]=> NULL ["option3"]=> NULL
  ["created_at"]=> object(DateTime)#59 (3) { ["date"]=> string(26)
  "2018-03-27 11:01:14.000000" ["timezone_type"]=> int(1) ["timezone"]=>
  string(6) "+03:00" } ["updated_at"]=> object(DateTime)#53 (3) {
  ["date"]=> string(26) "2018-03-27 11:01:14.000000" ["timezone_type"]=>
  int(1) ["timezone"]=> string(6) "+03:00" } ["taxable"]=> bool(true)
  ["barcode"]=> NULL ["grams"]=> int(0) ["image_id"]=> NULL
  ["inventory_quantity"]=> int(1) ["weight"]=> string(1) "0"
  ["weight_unit"]=> string(2) "kg" ["old_inventory_quantity"]=> int(1)
  ["requires_shipping"]=> bool(true) } ["data":protected]=> array(26) {
  ["barcode"]=> NULL ["compare_at_price"]=> NULL ["created_at"]=>
  object(DateTime)#59 (3) { ["date"]=> string(26) "2018-03-27
  11:01:14.000000" ["timezone_type"]=> int(1) ["timezone"]=> string(6)
  "+03:00" } ["fulfillment_service"]=> string(6) "manual" ["grams"]=>
  int(0) ["id"]=> int(7196665282625) ["image_id"]=> NULL
  ["inventory_management"]=> NULL ["inventory_policy"]=> string(4)
  "deny" ["old_inventory_quantity"]=> int(1) ["inventory_quantity"]=>
  int(1) ["inventory_quantity_adjustment"]=> NULL ["metafield"]=> NULL
  ["option1"]=> string(13) "Default Title" ["option2"]=> NULL
  ["option3"]=> NULL ["position"]=> int(1) ["price"]=> string(4) "0.00"
  ["product_id"]=> int(575833276481) ["requires_shipping"]=> bool(true)
  ["sku"]=> string(0) "" ["taxable"]=> bool(true) ["title"]=> string(13)
  "Default Title" ["updated_at"]=> object(DateTime)#53 (3) { ["date"]=>
  string(26) "2018-03-27 11:01:14.000000" ["timezone_type"]=> int(1)
  ["timezone"]=> string(6) "+03:00" } ["weight"]=> string(1) "0"
  ["weight_unit"]=> string(2) "kg" } ["types":protected]=> array(26) {
  ["barcode"]=> string(6) "string" ["compare_at_price"]=> string(6)
  "string" ["created_at"]=> string(8) "DateTime"
  ["fulfillment_service"]=> string(6) "string" ["grams"]=> string(7)
  "integer" ["id"]=> string(7) "integer" ["image_id"]=> string(7)
  "integer" ["inventory_management"]=> string(6) "string"
  ["inventory_policy"]=> string(6) "string" ["old_inventory_quantity"]=>
  string(7) "integer" ["inventory_quantity"]=> string(7) "integer"
  ["inventory_quantity_adjustment"]=> string(6) "string" ["metafield"]=>
  string(5) "array" ["option1"]=> string(6) "string" ["option2"]=>
  string(6) "string" ["option3"]=> string(6) "string" ["position"]=>
  string(7) "integer" ["price"]=> string(6) "string" ["product_id"]=>
  string(7) "integer" ["requires_shipping"]=> string(7) "boolean"
  ["sku"]=> string(6) "string" ["taxable"]=> string(7) "boolean"
  ["title"]=> string(6) "string" ["updated_at"]=> string(8) "DateTime"
  ["weight"]=> string(6) "string" ["weight_unit"]=> string(6) "string" }
  } } ["vendor"]=> string(7) "Printer" ["image"]=> NULL }
  ["types":protected]=> array(18) { ["body_html"]=> string(6) "string"
  ["created_at"]=> string(8) "DateTime" ["handle"]=> string(6) "string"
  ["id"]=> string(7) "integer" ["images"]=> string(14) "ProductImage[]"
  ["options"]=> string(15) "ProductOption[]" ["product_type"]=>
  string(6) "string" ["published_at"]=> string(8) "DateTime"
  ["published_scope"]=> string(6) "string" ["tags"]=> string(6) "string"
  ["template_suffix"]=> string(6) "string" ["title"]=> string(6)
  "string" ["metafields_global_title_tag"]=> string(6) "string"
  ["metafields_global_description_tag"]=> string(6) "string"
  ["updated_at"]=> string(8) "DateTime" ["variants"]=> string(16)
  "ProductVariant[]" ["vendor"]=> string(6) "string" ["image"]=>
  string(12) "ProductImage" } }

and I need to get product_id from this code, but when I try with echo $product->product_id; it give's me error like 

Property 'product_id' does not exist for Shopify\Object\Product

I can get only id, title, image and some other info with for example $product->title , I tried with $product->id->title but still nothing.

id is a private property. You need to use a getter function to access it. try something like $product->getId() if that even exists. Look at the source code of the object to see if there is a function that does return $this->id;

There's no product_id on product object, it's Shopify\Object\ProductVariant object's property.

echo $product->changedFields->id;

well the best practice is to use the getId in the object itself.

$ProductVariantObj->changeField['product_id'];  doesn't works? The problem is maybe with the private part

according to the API docs , you will notice that the product_id is inside images or variants.
so its more like  $product->variants[0]->product_id
https://help.shopify.com/api/reference/product

I tried now with
$variants = new Shopify\Object\ProductVariant();
$variants->product_id and $product->variants->product_id
with $variants->product_id give's me just Null, not error, but still don't work.

Works now with  $product->variants[0]->product_id. Thank's  everyone :)
