2016-05-18 5 views
2

JSONファイルの特定のエントリを選択するにはどうすればよいですか?多次元json php

{ 
    "status" : "success", 
    "data" : { 
    "items" : [ 
     { 
     "market_hash_name" : "AK-47 | Redline (Field-Tested)", 
     "total_items" : 923, 
     "lowest_price" : "4.00", 
     "highest_price" : "300.00", 
     "cumulative_price" : "8722.77", 
     "recent_sales_info" : { 
      "hours" : "18.07", 
      "average_price" : "4.21" 
     } 
     }, 
     { 
     "market_hash_name" : "AK-47 | Redline (Minimal Wear)", 
     "total_items" : 51, 
     "lowest_price" : "14.26", 
     "highest_price" : "100.00", 
     "cumulative_price" : "1089.71", 
     "recent_sales_info" : { 
      "hours" : "23.37", 
      "average_price" : "14.36" 
     } 
     } 
    ] 
    } 
} 

スクリーンショット:

enter image description here

私のPHPスクリプト:

$steam = "AK-47 | Redline (Field-Tested)"; 
$link = 'list.json'; 
    $string = file_get_contents($link); 
    $obj = json_decode($string); 
    if($obj->{'status'} == "success") die("notfound"); 
    $lowest_price = $obj->{'lowest_price'}; 
    $lowest_price[strlen($lowest_price)] = 0; 
    echo $lowest_price; 

がどのように私は例えば "TOTAL_ITEMS" とAK-の "平均価格" を選択することができます47レッドライン(実地試験済み)? market_hash_nameは変数に保存され、関連する値を他の変数に保存します。

ありがとうございます。 よろしく。 Enge

+0

'json_decode($ strの、真の)'あなたに配列 –

答えて

0

json_decodeとforeachを使用すると、必要なものを達成できます。

Online Check、確認してお知らせください。

$json = '{ 
    "status" : "success", 
    "data" : { 
    "items" : [ 
     { 
     "market_hash_name" : "AK-47 | Redline (Field-Tested)", 
     "total_items" : 923, 
     "lowest_price" : "4.00", 
     "highest_price" : "300.00", 
     "cumulative_price" : "8722.77", 
     "recent_sales_info" : { 
      "hours" : "18.07", 
      "average_price" : "4.21" 
     } 
     }, 
     { 
     "market_hash_name" : "AK-47 | Redline (Minimal Wear)", 
     "total_items" : 51, 
     "lowest_price" : "14.26", 
     "highest_price" : "100.00", 
     "cumulative_price" : "1089.71", 
     "recent_sales_info" : { 
      "hours" : "23.37", 
      "average_price" : "14.36" 
     } 
     } 
    ] 
    } 
}'; 
$result = json_decode ($json); 

foreach($result->data->items as $val){ 
    if($val->market_hash_name == "AK-47 | Redline (Field-Tested)") 
     echo "Total Item: ".$val->total_items." AND Avg Price:".$val->recent_sales_info->average_price; 
    }  
} 

結果は次のとおりです。総アイテム:923と平均価格:4.21

+1

パーフェクトを与え、私はすべてのフォームを取得できますか – Enge

+0

ありがとうございましたエントリはaverage_priceとtotal_items? – Enge

+0

その上に、すべてのエントリが必要な場合は、in条件を削除するだけです。 –