2016-05-17 24 views
0

「total_items」と「average_price」の数を選択するにはどうすればよいですか?デコードJSON(PHP)

JSON:私のPHPスクリプトザッツ

{ 
    "status" : "success", 
    "data" : { 
    "items" : [ 
     { 
     "market_hash_name" : "AK-47 | Redline (Field-Tested)", 
     "total_items" : 698, 
     "lowest_price" : "3.90", 
     "highest_price" : "300.00", 
     "cumulative_price" : "4669.62", 
     "recent_sales_info" : { 
      "hours" : "17.94", 
      "average_price" : "4.23" 
     } 
     } 
    ] 
    } 
} 

:私は、変数に "TOTAL_ITEMS" の数と "average_price" を保存することができますどのように

$link = 'skin.json'; 
    $string = file_get_contents($link); 
    $obj = json_decode($string, TRUE); 
$name = $obj['items']['market_hash_name']; 
$itmes = $name['total_items']; 
$itmes = $name['average_price']; 

? ありがとうございます よろしく。 エンゲ

+0

$ obj ['data'] ['items'] [0] ['total_items'] 'と' $ obj ['data'] [ 'items'] [0] ['recent_sales_info '] [' average_price '] '。 – showdev

答えて

0

はこれを試してみてください:

$total_items = $obj['data']['items'][0]['total_items']; 
$avg_price = $obj['data']['items'][0]['recent_sales_info']['average_price']; 
+0

少し話題にはなりませんが、あなたの姓は普通ですか?一度同じ姓の講師がいた。 –

+0

それは正しいですか?:$ link = 'skin.json'; $ string = file_get_contents($ link); $ obj = json_decode($ string、TRUE); $ total_items = $ obj ['data'] ['items'] [0] ['total_items'];$ avg_price = $ obj ['データ'] ['items'] [0] ['recent_sales_info'] ['average_price']; echo $ total_items; echo $ avg_price; – Enge

+0

@ Engeはい。代わりに、コンテキストに応じて配列にエコーすることができます –

1

アンドレアスが書いた、または同じように:

// first, new variable (shorter lines/less depth afterwards) 
$article = $obj['data']['items'][0]; 

// then 
$total_items = $article['total_items']; 
$avg_price = $article['recent_sales_info']['average_price']; 

エンゲ、 "アイテム" ことに注意してください:[はそれだ、JSONオブジェクト内の開口部の配列ですあなたは欠けていました...