1.数组转json字符串

$myArray = array(
  "id" => "abc",
  "name" => "hello woody"
);
echo json_encode($myArray);

输出结果为

{id:'abc',name:'hello woody'}

2.json字符串转数组对象

$jsonObj = json_decode("{id:'abc',name:'hello woody'}");

结果为

echo $jsonObj->id;
echo $jsonObj->name;

3.sql查询结果转json对象
在php中,查询表返回结果是array数组类型,将返回的结果用json_encode方法转成json字符串

$sql = "查询数据";
$query = $this->db->query($sql);
$this->output->set_output(json_encode($query->result()));

hellowoody
118 声望10 粉丝

个人网站:woodyhello.com