首页 文章

如果之前没有访问过数组,则json_encode返回null

提问于
浏览
2

在我的代码中,我构建了一个数组并使用json_encode对其进行编码,json_encode为此数组返回null,除非我插入指令“echo $ responce-> rows [0] [0];”在编码之前,如果我注释掉这一行,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]}]}

1 回答

  • 2

    如果您的数组值未编码为 utf8 ,则 json_encode 将返回 null (因此它们对 json_encode 不安全)

    如果从db获取数据,请尝试使用:

    SELECT 陈述之前 mysql_query('SET CHARACTER SET utf8')

相关问题