php json和字符串互转

记录一下:

'jinan','henan'=>'zhengzhou','hebei'=>'shijiazhuang');
$jn_city = json_encode($city);
 
//反编码json
$fan_city = json_decode($jn_city,false);//第二个参数false则返回object类型,false可以默认不写
var_dump($fan_city);//object(stdClass)#1 (3) { ["shandong"]=> string(5) "jinan" ["henan"]=> string(9) "zhengzhou" ["hebei"]=> string(12) "shijiazhuang" } 
echo "
"; $fan_city = json_decode($jn_city,true);//第二个参数true则返回array类型 var_dump($fan_city);//array(3) { ["shandong"]=> string(5) "jinan" ["henan"]=> string(9) "zhengzhou" ["hebei"]=> string(12) "shijiazhuang" }

你可能感兴趣的:(php json和字符串互转)