2015-05-25 41 views
0

我真的有麻烦,试图找出为什么这不起作用,它看起来应该很容易,但似乎无法得到它。从对象内部打印值

我所要做的就是抓住sku字段(下面是VBP-01)。

{ 
 
    "variants": [ 
 
     { 
 
      "id": 2314578, 
 
      "created_at": "2014-07-29T07:22:18.921Z", 
 
      "updated_at": "2015-05-21T15:42:42.136Z", 
 
      "product_id": 1188647, 
 
      "default_ledger_account_id": null, 
 
      "buy_price": "124.0", 
 
      "committed_stock": "0", 
 
      "incoming_stock": "3", 
 
      "composite": false, 
 
      "description": null, 
 
      "is_online": false, 
 
      "keep_selling": false, 
 
      "last_cost_price": "124.0", 
 
      "manage_stock": true, 
 
      "max_online": null, 
 
      "moving_average_cost": "124", 
 
      "name": "Lanparte battery pinch VBP-01", 
 
      "online_ordering": false, 
 
      "opt1": null, 
 
      "opt2": null, 
 
      "opt3": null, 
 
      "position": 1, 
 
      "product_name": "Lanparte V-Mount Battery Pinch", 
 
      "product_status": "active", 
 
      "product_type": null, 
 
      "retail_price": "0.0", 
 
      "sellable": true, 
 
      "sku": "VBP-01", 
 
      "status": "active", 
 
      "stock_on_hand": "1", 
 
      "supplier_code": "VBP-01", 
 
      "taxable": true, 
 
      "upc": null, 
 
      "weight": null, 
 
      "wholesale_price": "0.0", 
 
      "image_ids": [], 
 
      "variant_prices": [ 
 
       { 
 
        "price_list_id": "buy", 
 
        "value": "124.0" 
 
       }, 
 
       { 
 
        "price_list_id": "retail", 
 
        "value": "0.0" 
 
       }, 
 
       { 
 
        "price_list_id": "wholesale", 
 
        "value": "0.0" 
 
       } 
 
      ], 
 
      "locations": [ 
 
       { 
 
        "location_id": 16377, 
 
        "stock_on_hand": "1", 
 
        "committed": null, 
 
        "incoming": "3", 
 
        "bin_location": null, 
 
        "reorder_point": 3 
 
       } 
 
      ], 
 
      "prices": { 
 
       "buy": "124.0", 
 
       "retail": "0.0", 
 
       "wholesale": "0.0" 
 
      }, 
 
      "stock_levels": { 
 
       "16377": "1.0" 
 
      }, 
 
      "committed_stock_levels": {}, 
 
      "incoming_stock_levels": { 
 
       "16377": "3.0" 
 
      } 
 
     } 
 
    ], 
 
    "meta": { 
 
     "total": 1 
 
    } 
 
}

目前我使用下面的代码,没有运气

$url = "https://api.tradegecko.com/variants?sku=VBP-01"; 
 
$data = file_get_contents($url, false, $context); 
 

 
$json = json_decode($data); 
 

 
print $json->{'variant'}->{'sku'};

我做错了吗?

+0

这并不完全清楚,我为什么有些整型变量有其价值的报价,而有的则没有。 – jcoppens

+0

对此不确定,但我只能假设这些产品不存在这些值。只有null,false和true是数组中不包含引号 –

+0

的变体。可能有任何数量的 – njzk2

回答

0

只是

echo $json->{'variants'}[0]->{'sku'}; 

在循环

foreach ($json->variants as $variants) { 
    echo $variants->sku; 
} 
+0

非常感谢!你能解释为什么吗? –

+1

“变体”只是一个数组 – Federkun