當然,JSONSerialize PHP不僅僅能夠序列化對象,還可以序列化數組、列表等數據類型。示例代碼如下:$students = [
new Person("Lucy", "female", 23),
new Person("Tom", "male", 24),
new Person("Lily", "female", 22)
];
$json = json_encode($students);
echo $json;
上面的代碼中,我們創建了一個$students數組,并將三個Person對象添加到數組中。然后,我們使用json_encode()方法將$students序列化成JSON格式的字符串。輸出結果如下:[
{"name":"Lucy","gender":"female","age":23},
{"name":"Tom","gender":"male","age":24},
{"name":"Lily","gender":"female","age":22}
]
可以看到,$students數組中所有的Person對象都被成功地序列化成了JSON格式的字符串。以上就是關于JSONSerialize PHP的簡單介紹和使用方法。希望能對大家有所幫助。