2014-12-30 44 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