JSON转数组 获取有关问题
JSON转数组 获取问题
我这个是 用json 转成数组
输出出来是:
Array ( [0] => stdClass Object ( [省] => 台湾 [市] => Array ( [0] => stdClass Object ( [市名] => 台北县 [编码] => 101340101 ) [1] => stdClass Object ( [市名] => 台中 [编码] => 101340401 ) ) ) )
我要的格式是
echo 编码=>城市
这样的数组我不知道怎么获取 省 市 编码了 有没有大神 能获取 这3种数据的呀
------解决方案--------------------
json_decode($cityArry, true);
------解决方案--------------------
<?php
$cityArry = '[
{
"省": "台湾",
"市": [
{
"市名": "台北县",
"编码": "101340101"
},
{
"市名": "台中",
"编码": "101340401"
}
]
}
]';
$temp1 = json_decode($cityArry);
$arr = array ($temp1);
print_r($arr[0]);
?>
我这个是 用json 转成数组
输出出来是:
Array ( [0] => stdClass Object ( [省] => 台湾 [市] => Array ( [0] => stdClass Object ( [市名] => 台北县 [编码] => 101340101 ) [1] => stdClass Object ( [市名] => 台中 [编码] => 101340401 ) ) ) )
我要的格式是
echo 编码=>城市
这样的数组我不知道怎么获取 省 市 编码了 有没有大神 能获取 这3种数据的呀
JSON
数组
------解决方案--------------------
json_decode($cityArry, true);
------解决方案--------------------
$arr = json_decode($cityArry, true);
foreach($arr[0] as $key=>$item) {
echo $item['省'], PHP_EOL;
foreach($item['市'] as $row) {
echo "$row[编码] => $row[市名]\n";
}
}