2011-03-09 51 views
2

在我的代码中,我建立一个数组并使用json_encode进行编码, 除非插入指令“echo $ responce-> rows [0] [0]; “在编码之前,如果我注释掉这行json_encode返回null!json_encode返回null如果数组未被访问

有人可以找出原因吗?

echo $responce->rows[0][0]; 
echo json_encode($responce); 

它有这个编码的JSON这种行为,但适用于不同的阵列:

{"rows":[{"id":"33UD","cell":["Great Yarmouth Borough Council",5875732.23,61.01]}, 
{"id":"41UE","cell":["Newcastle-Under-Lyme District Council",2514111.76,20.24]}, 
{"id":"36UF","cell":["Ryedale District Council",96439.18,1.8]},{"id":"00CM","cell": 
["Sunderland City Council",16473262.71,58.48]}]} 
+0

你能添加更多的代码吗? – 2011-03-09 11:09:49

+0

'var_dump($ responce)'输出了什么? – Jon 2011-03-09 11:14:52

+0

你从数据库中获取数据吗? – thedev 2011-03-09 11:22:32

回答

2

您将获得null通过json_encode返回如果您的数组值不编码为utf8(所以它们不是安全的json_encode

如果使用来自DB尝试让您的数据:

mysql_query('SET CHARACTER SET utf8') 之前SELECT声明