2016-11-14 217 views
1

我試圖從數組中獲取json_objects。我得到我想要轉換的數組,但json_encode不返回任何內容。php json_encode不會返回任何東西

$result = mysqli_query($conn, $query); 
$i = 1; 
$country = array(); 
$countries = array(); 
if(mysqli_num_rows($result) > 0){ 
    while($row = mysqli_fetch_array($result)){ 
     echo $row['idPais']." "; 
     echo $row['nombre']."<br>"; 

     $country = array(
       'idPais' => $row['idPais'], 
       'nombre' => $row['nombre'] 
     ); 
     array_push($countries, $country); 
    } 
    print_r($countries); 

    echo json_encode($countries,JSON_FORCE_OBJECT); 
    }else{ 
    echo "false"; 
} 

的print_r($國家)返回此陣:

Array ([0] => Array ([idPais] => 7 [nombre] => Espa�a) 
     [1] => Array ([idPais] => 8 [nombre] => Francia) 
     [2] => Array ([idPais] => 9 [nombre] => Portugal) 
) 
+1

你可以告訴西班牙人不要把他們的語言寫成'español',或者你可以用UFT-8連接到你的數據庫。 – Xorifelse

+0

如果我直接將'json_encode'應用到數組,它對我有用。我有PHP7。 –

+1

可能會幫助:[json_last_error()](http://php.net/manual/en/function.json-last-error.php) – simon

回答

0

的問題是,我用 'N' 卡拉科特。爲了使用它與數據庫,我不得不把這一行在我的代碼:

mysqli_set_charset($ conn,「utf8」);

謝謝大家。