"; //一维数组转换json $arr1 = array("username"=>"zhangsan","age"=>"100"); $arr12Json = json_encode($arr1); print_r($arr1); echo "
"; print_r($arr12Json); //二维数组转换json $arr2 = array(array("name"=>"zhangsan","age"=>"25"), array("name"=>"lisi","age"=>"26"), ); $arr2tojson = json_encode($arr2); echo "
"; print_r($arr2)."
";; echo "
"; print_r($arr2tojson); //对象转换到json(只转换共有变量,私有变量和函数不转换) class demo { public $public_name = "piblic name"; protected $protected_name = "protected name"; private $private_name ="private name"; public function getName() { return $this->name; } } $demo = new demo(); $objToJson = json_encode($demo); echo "
"; print_r($demo); print_r($objToJson); ?>
输出:
以上就介绍了数组和对象转换为json,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
已抢186364个
抢已抢67219个
抢已抢352853个
抢已抢666941个
抢已抢121881个
抢已抢22891个
抢