2014-12-30 40 views
0

我想要json_encode一个php数组,但“选择”部分返回null。json_encode在数组上返回null

Array ([indexer] => 7 [section] => 1 [question] => What does the last paragraph indicate about an optimistic perspective? [answer] => a [choices] => There has never been a culture or religion that is genuinely optimistic`It is “a riddle of the universe”`No enlightened culture sees the world in a truly optimistic manner`Optimistic perspectives are only held by the weak) 

{"indexer":"7","section":"1","question":"What does the last paragraph indicate about an optimistic perspective?","answer":"a","choices":null} 

有什么想法为什么?

编辑:全码:

$check = mysqli_query($con, "select * from questions where section = '$section' and indexer = '$question'"); 
$result = mysqli_fetch_array($check, MYSQLI_ASSOC); 

print_r($result); 
echo json_encode($result); 
+0

听起来像它不喜欢在文本值。 –

+1

我明白这一点。即时通讯设法弄清楚为什么 – user2570937

+0

你能澄清一下:你是来自'print_r'还是什么的第一行('Array ...')?显示你如何定义该数组。你确定要逃避所有这些引号吗? – philtune

回答

1

json_encode期望你编码为有效的UTF-8的所有内容。如果内容的任何部分是不妥当的UTF-8,json_encode将失败默默的那部分(B

所有字符串数据必须是UTF-8编码。

我的猜测是, 马克未正确UTF-8编码的(但是从CP1252基于编码起源)。

参见json_last_error从JSON模块检索最后错误代码。

+0

即时获取bool(JSON_ERROR_UTF8上的true),我的列是utf8_general_ci。如何将UTF-8中的标记正确编码? – user2570937

+0

相关部分是MySQL连接的字符集 - 如果还没有设置为UTF8(使用SET NAMES“utf8”或者使用PDO或mysqli中的charset属性)在插入和检索时,你会得到fscked的编码,你可以使用iconv从一个字符集转换到另一个字符串,如果需要的话可以递归地执行。 – MatsLindh

+0

mysqli_set_charset($ con,“utf8”); working – user2570937