.PHP.Json 包含西里尔文数据的 NULL 值


PHP. Json contains NULL values for data in cyrillic

我正在尝试获取JSON格式的数据。在数据库中,"雇员"表包含西里尔文的数据

<?php
$mysqli = new mysqli("localhost","user","password","db");
    $myArray = array();
    if ($result = $mysqli->query("SELECT * FROM employee")) {
        $tempArray = array();
        while($row = $result->fetch_object()) {
                $tempArray = $row;
                array_push($myArray, $tempArray);
            }
        echo json_encode($myArray);
    }
    $result->close();
    $mysqli->close();
?>

结果包含 NULL 而不是西里尔文中的值。

[
{
id: "1",
lastname: null,
firstname: null,
middle: null,
occupation: null,
dob: "1991-01-01",
mobile: "+99999 9999999",
home: "+77777 7777777",
email: "testmail@mail.ru"
}
]

myArray 的输出:

a:2:{i:0;O:8:"stdClass":9:{s:2:"id";s:1:"1";s:8:"lastname";s:7:"Азизов";s:9:"firstname";s:6:"Азиз";s:6:"middle";s:

14:"Азизович";s:10:"occupation";s:19:"Android разработчик";s:3:"dob";s:10:"1991-01-01

";s:6:"mobile";s:13:"+99999999999";s:4:"home";s:13:"+777777777777";s:5:"email";s:22:"testmail@mail.ru";}}

如何解决这个问题?

我刚刚添加了$mysqli->set_charset("utf8"); 这行代码将数据编码为 UTF-8 格式。为什么它不起作用?因为json_encode需要 UTF-8 数据

<?php
$mysqli = new mysqli("localhost","user","password","db");
$mysqli->set_charset("utf8");
    $myArray = array();
    if ($result = $mysqli->query("SELECT * FROM employee")) {
        $tempArray = array();
        while($row = $result->fetch_object()) {
                $tempArray = $row;
                array_push($myArray, $tempArray);
            }
        echo json_encode($myArray);
        //echo serialize($myArray);
    }
    $result->close();
    $mysqli->close();
?>