<?php echo 'array-json:' . " "; $arr = array('key1'=>'value1', 'key2' => 'value2', 'aa'); echo json_encode($arr); /* { "key1":"value1", "key2":"value2", "0":"aa" } */ echo " "; $arr2 = array('key1'=>'value1', 'kk'=>array('key2'=>'value2', 'key3'=>'value3')); echo json_encode($arr2); /* { "key1":"value1", "kk": { "key2":"value2", "key3":"value3" } } */ echo " " . 'json-object' . " "; class t { public $name = 'lin3615'; function ok() { $this->name = 'new lin3615'; return $this->name; } } $t = new t(); echo json_encode($t); /* { "name":"lin3615" } */ echo " " . json_encode($t->ok()); // "new lin3615" echo " "; $sss = '{"bar":"value"}'; print_r(json_decode($sss, true)); echo " "; $ny = array('a', 'b', 'c'); print_r(json_encode($ny)); /* [ "a", "b", "c" ] */ /* 总结:由于json只支持索引数据,不支持关联数组,当为关联数组时,会转化为对象 {}的形式,[]索引数组的形式 */ // 序列化数据与对象 echo 'serialize-array:' . "<br />"; $arr = array('key1'=>'value1', '$key2'=>'value2', 'aa'); echo serialize($arr); // a:3:{s:4:"key1";s:6:"value1";s:5:"$key2";s:6:"value2";i:0;s:2:"aa";} echo "<br />serialize-object:<br />"; class test { public $name='lin3615'; public $age = 27; function ok() { return 1; } function nn() { } } $t = new test(); echo serialize($t); // O:4:"test":2:{s:4:"name";s:7:"lin3615";s:3:"age";i:27;} echo serialize($t->ok()); // i:1; echo serialize($t->nn()); // N;