只有变量可以通过引用- array_walk传递。

时间:2023-01-06 16:02:35

I'm trying to build JSON from DB table, I need to use utf8_encode() before json_encode(). I go t this error. Here is a code. What I'm doing wrong?

我正在尝试从DB表构建JSON,我需要在json_encode()之前使用utf8_encode()。我不考虑这个错误。这是一个代码。我做错了什么吗?

    <?php
$db = new PDO('mysql:host=MY_HOST;dbname=MY_DB;charset=UTF-8',  
    'MY_LOGIN', 'MY_PASS', array(  
        PDO::ATTR_EMULATE_PREPARES => false,  
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION  
    )  
);    

$stmt = $db->prepare(  
    "SELECT id,title,`fulltext`,catid,publish_state  FROM items WHERE id>:id"  
);  
$stmt->bindValue(':id', 2000, PDO::PARAM_STR);  
$stmt->execute();  
$output_arr = $stmt->fetchAll(PDO::FETCH_ASSOC);

//FOR TEST ONLY
echo 'Output:<br />';
print_r ($output_arr); //prints OK if I remove line 21 (below)

//BEFORE ENCODING TO JSON I NEED TO ENCODE TO UTF-8...
$output_encoded = array_walk(utf8_encode, $output_arr);

//AND NOW I GOT ERROR:
//Only variables can be passed by reference in line 21

//MY NEXT STEP WOULD BE:
echo json_encode($output_encoded)
?>

1 个解决方案

#1


1  

If you'd fix your PDO connection string, you'd already be receiving UTF-8 encoded data directly from the database:

如果你修复你的PDO连接字符串,你将会直接从数据库接收UTF-8编码的数据:

$db = new PDO('mysql:host=MY_HOST;dbname=MY_DB;charset=utf8', ...
                                                       ^^^^

The charset is called "utf8" in MySQL, not UTF-8. Having set this charset parameter correctly, $stmt->fetchAll(PDO::FETCH_ASSOC) will already return UTF-8 encoded data and there's no need to use utf8_encode at all.

该字符集在MySQL中称为“utf8”,而不是UTF-8。正确设置这个字符集参数后,$stmt->fetchAll(PDO:::FETCH_ASSOC)将已经返回UTF-8编码的数据,根本不需要使用utf8_encode。


As for your particular issue, you had the wrong syntax, it should have been:

至于你的问题,你的语法错误,应该是:

array_walk($output_arr, 'utf8_encode')

Even that wouldn't have helped though, since the callback for array_walk needs to modify the parameter by reference, which utf8_encode does not do. You probably meant to do this:

即使这样也不会有帮助,因为array_walk的回调需要通过引用修改参数,而utf8_encode没有这样做。你可能想这么做:

$output_encoded = array_map('utf8_encode', $output_arr);

But even that wouldn't be correct, since $output_arr is a multidimensional array which would not be treated correctly here. And again, by fixing the PDO connection that's unnecessary anyway. In fact, do not do this when your database is already returning proper UTF-8, it'll just garble your data.

但即使这样也不正确,因为$output_arr是一个多维数组,在这里不能正确处理。同样,通过修复PDO连接,这是不必要的。实际上,当数据库已经返回正确的UTF-8时,不要这样做,它只会让你的数据变得模糊。

#1


1  

If you'd fix your PDO connection string, you'd already be receiving UTF-8 encoded data directly from the database:

如果你修复你的PDO连接字符串,你将会直接从数据库接收UTF-8编码的数据:

$db = new PDO('mysql:host=MY_HOST;dbname=MY_DB;charset=utf8', ...
                                                       ^^^^

The charset is called "utf8" in MySQL, not UTF-8. Having set this charset parameter correctly, $stmt->fetchAll(PDO::FETCH_ASSOC) will already return UTF-8 encoded data and there's no need to use utf8_encode at all.

该字符集在MySQL中称为“utf8”,而不是UTF-8。正确设置这个字符集参数后,$stmt->fetchAll(PDO:::FETCH_ASSOC)将已经返回UTF-8编码的数据,根本不需要使用utf8_encode。


As for your particular issue, you had the wrong syntax, it should have been:

至于你的问题,你的语法错误,应该是:

array_walk($output_arr, 'utf8_encode')

Even that wouldn't have helped though, since the callback for array_walk needs to modify the parameter by reference, which utf8_encode does not do. You probably meant to do this:

即使这样也不会有帮助,因为array_walk的回调需要通过引用修改参数,而utf8_encode没有这样做。你可能想这么做:

$output_encoded = array_map('utf8_encode', $output_arr);

But even that wouldn't be correct, since $output_arr is a multidimensional array which would not be treated correctly here. And again, by fixing the PDO connection that's unnecessary anyway. In fact, do not do this when your database is already returning proper UTF-8, it'll just garble your data.

但即使这样也不正确,因为$output_arr是一个多维数组,在这里不能正确处理。同样,通过修复PDO连接,这是不必要的。实际上,当数据库已经返回正确的UTF-8时,不要这样做,它只会让你的数据变得模糊。