如何从内容中获取值?

时间:2015-06-24 18:53:18

标签: php

{" prereqs" {"前提条件" {"类型":" prereq_check""值" :" submerging_island_feature_enabled"}},"区划" {"除法":[{"项目" {"项目&#34 ;:[{"名称":" rhino_shell""稀有":"公共"},{"名称" :" walrus_wavy""稀有":"特别"},{"名称":" hippo_fancyshell"&# 34;稀有":"稀有"},{"名称":" rhino_jellyfish""稀有":" superRare& #34;}]},"名称":" ruby​​Count_30"},{"项目" {"项目":[{&# 34;名称":" walrus_clam""稀有":"公共"},{"名称":" hippo_nautical& #34;"稀有":"特别"},{"名称":" giraffe_coral""稀有" :"稀有"},{"名称":" elephant_starburst""稀有":" superRare"}]} "名称":" ruby​​Count_40"},{"项目" {"项目":[{"名称" :" GIRA ffe_waverider""稀有":"公共"},{"名称":" pony_sea""稀有&#34 ;:"特别"},{"名称":" magicdeer_seadeer""稀有":"稀有"}, {"名称":" pony_seaprincesscorn""稀有":" superRare"}]},"名称":& #34; ruby​​Count_50"},{"项目" {"项目":[{"名称":" bigcat_crystallion",& #34;稀有":"公共"},{"名称":" magicdeer_midnightdeer""稀有":"特别"},{"名称":" horse_ofthesea""稀有":"稀有"},{"名称& #34;:" horse_wingedsea""稀有":" superRare"}]},"名称":" ruby​​Count_60&#34 ;}]},"各具特色" {"配方" {"配方":[{"名称":" qdke& #34;},{"名称":" sb1p"},{"名称":" cb8v"}]}},&# 34; listEndDate":&#34 07 /二千零十五​​分之十三"" currencyItem" {"名称":" healingpotionbottle"},& #34; FE ED" {" throttleTime":" 21600"}"名称":" submerging_island"}

1 个答案:

答案 0 :(得分:0)

让你花2美分。首先,欢迎您,请阅读How to ask a good question

首先,您需要将json字符串解码为数组。使用该数组,您可以获得值。

<?php

$json = '{"prereqs":{"prereq":{"type":"prereq_check","value":"submerging_island_feature_enabled"}},
"divisions":{"division":[{"items":{"item":[{"name":"rhino_shell","rarity":"common"},
{"name":"walrus_wavy","rarity":"special"},{"name":"hippo_fancyshell","rarity":"rare"},
{"name":"rhino_jellyfish","rarity":"superRare"}]},"name":"rubyCount_30"},
{"items":{"item":[{"name":"walrus_clam","rarity":"common"},{"name":"hippo_nautical","rarity":"special"},
{"name":"giraffe_coral","rarity":"rare"},{"name":"elephant_starburst","rarity":"superRare"}]},"name":"rubyCount_40"},
{"items":{"item":[{"name":"giraffe_waverider","rarity":"common"},{"name":"pony_sea","rarity":"special"},
{"name":"magicdeer_seadeer","rarity":"rare"},
{"name":"pony_seaprincesscorn","rarity":"superRare"}]},"name":"rubyCount_50"},
{"items":{"item":[{"name":"bigcat_crystallion","rarity":"common"},{"name":"magicdeer_midnightdeer","rarity":"special"},
{"name":"horse_ofthesea","rarity":"rare"},
{"name":"horse_wingedsea","rarity":"superRare"}]},"name":"rubyCount_60"}]},"crafting":{"recipes":{"recipe":[{"name":"qdke"},
{"name":"sb1p"},{"name":"cb8v"}]}},"listEndDate":"07/13/2015","currencyItem":{"name":"healingpotionbottle"},"feed":{"throttleTime":"21600"},"name":"submerging_island"}';

//decode the json
$decoded = json_decode($json, true);


// uncomment if you want it to be easier to read
// echo "<pre>";
// print_r($decoded);
// echo "</pre>";

//if you want the singe value, i.e. name of the first item.
echo "If you want a sinle value:<br>";
echo $decoded['divisions']['division'][0]['items']['item'][0]['name'] . "<br>";

//to get all names from one item  you need to use a foreach() loop. 
echo "<br>if you want all names from one item:<br>";
foreach($decoded['divisions']['division'][0]['items']['item'] AS $value){
    echo $value['name'] . "<br>";
}

//to get all names we need to use 2 foreach loops because this is nested in multiple arrays
echo "<br>if you want all names from all items:<br>";
foreach($decoded['divisions']['division'] AS $value){
    foreach($value['items']['item'] AS $value){
        echo $value['name'] . "<br>";
    }
}

?>