php 对象数组转json字符串5

来源:互联网 发布:菱王电梯怎么样知乎 编辑:程序博客网 时间:2024/05/27 00:33
Php代码  收藏代码
  1. <?php  
  2.     header("content-type:text/html; charset=utf-8");  
  3.     class Student {  
  4.         private $number;  
  5.         private $name;  
  6.         private $score;  
  7.         function __construct($number,$name,$score){  
  8.             $this->number=$number;  
  9.             $this->name=$name;  
  10.             $this->score=$score;  
  11.         }  
  12.         public function getNumber() {  
  13.             return $this->number;  
  14.         }  
  15.       
  16.         public function getName() {  
  17.             return $this->name;  
  18.         }  
  19.       
  20.         public function getScore() {  
  21.             return $this->score;  
  22.         }  
  23.       
  24.         public function setNumber($number) {  
  25.             $this->number = $number;  
  26.         }  
  27.       
  28.         public function setName($name) {  
  29.             $this->name = $name;  
  30.         }  
  31.       
  32.         public function setScore($score) {  
  33.             $this->score = $score;  
  34.         }  
  35.   
  36.     }  
  37.         $student1 = new Student("01","汤姆",80);  
  38.         $student2 = new Student("02","杰瑞",85);  
  39.         $arr =array();  
  40.         array_push($arr$student1);  
  41.         array_push($arr$student2);  
  42.         print_r($arr);  
  43.         $json_string = json_encode($arr,JSON_UNESCAPED_UNICODE);  
  44.         echo $json_string;   
  45.       
  46. ?>  

Php代码  收藏代码
  1. 结果为  
  2. X-Powered-By: PHP/5.5.7  
  3. Set-Cookie: ZendDebuggerCookie=127.0.0.1%3A10137%3A0||084|77742D65|1020; path=/  
  4. content-type:text/html; charset=utf-8  
  5.   
  6. Array  
  7. (  
  8.     [0] => Student Object  
  9.         (  
  10.             [number:Student:private] => 01  
  11.             [name:Student:private] => 汤姆  
  12.             [score:Student:private] => 80  
  13.         )  
  14.   
  15.     [1] => Student Object  
  16.         (  
  17.             [number:Student:private] => 02  
  18.             [name:Student:private] => 杰瑞  
  19.             [score:Student:private] => 85  
  20.         )  
  21.   
  22. )  
  23.   
  24. [{},{}]  

按官方的说法,私有变量无法被序列化出来
但是我并没有直接访问私有变量啊
如想得到以下结果,该怎么做?
0 0
原创粉丝点击