2015-09-22 16 views
0

我想將從數據庫檢索到的數據編碼到json中。這裏是我的代碼..var_dump顯示正確的數組,但json_encode函數嘗試編碼相同的數組時返回false

<?php 
$con = mysqli_connect("localhost", "root", "", "joomla");  

if($con === false){ 
    die("ERROR: Could not connect. " . mysqli_connect_error()); 
}  

$sql = "SELECT * FROM yv084_content"; 
$result = mysqli_query($con, $sql); 

$all_result = array(); 

$intro_result = array(); 

$num_rows = mysqli_num_rows($result); 

$count =0; 
while($row = mysqli_fetch_assoc($result)){   
    $all_result[] = $row;  
}  

while($count<$num_rows){   
    $intro_result[] = strip_tags($all_result[$count]['introtext']); 
    //echo strip_tags($all_result[$count]['introtext'])."<hr />"; 
    $count++; 
} 

echo "<hr />"; 
var_dump($intro_result); 
echo "<hr />"; 
$encoded_value = json_encode($intro_result); 

var_dump($encoded_value); 
echo $encoded_value; 

當我使用var_dump功能,然後我得到正確的結果,但是當我使用json_encode功能編碼陣列它返回假的..是否有任何想法如何對其進行編碼..

注意:有很多特殊字符和html實體,所以我使用strip_tags函數。

請查看結果我在瀏覽器中得到了...

Screenshot of browser's result

+0

可以粘貼在這裏'的var_dump($ intro_result)的結果;'這裏好嗎? –

+0

是的,只是給出數組的結果,所以我們可以找到你的東西 –

回答

1

按照上面的回答,你不能在數組轉換特殊字符json_encode。然而,你可以嘗試以下操作:

$d1 = array("asdadlasdladlasdad adadasdsa", "Nuwakot, Sep. 18: dadasda", "Åland Islands", "<b>test</b>"); 
array_walk($d1, function(&$value) { 
    $value = utf8_encode($value); 
}); 
print_r(json_encode($d1)); 
0

因爲如果你嘗試轉換的非utf8字符數組,你會得到0的返回值

json-encode

相關問題