php json_encode 不返回任何内容

标签 php arrays json

我正在尝试从数组中获取 json_objects。我得到了我想要转换的数组,但 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($countries) 返回此数组:

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

最佳答案

问题是我使用了“ñ”字符。为了将其与数据库一起使用,我必须将此行放入我的代码中:

mysqli_set_charset($conn, "utf8");

谢谢大家。

关于php json_encode 不返回任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40592359/

相关文章:

php - 简单的动态面包屑

php - 获取MySQL在特定时间添加的信息

c# - 排序数组的二分查找

javascript - 解析 JSON 并使用 jQuery mobile 中的数据扩展 ListView

javascript - 解析和迭代具有多个 header 的 JSON

php - 使用 artisan 输出记录 Laravel

php - 如何用PHP动态创建文件夹

javascript - 我无法从 php 打开模式

arrays - 将函数应用于多维数组 : R vs MATLAB

javascript - Ajax Post 请求返回 JSON 但延迟失败