php json_encode 部分不适用于数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19999665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:20:59  来源:igfitidea点击:

php json_encode not working on arrays partially

phpjson

提问by vsriram92

I have a PHP code which needs to encode DB table datas into json. So I used json_encode().

我有一个 PHP 代码,需要将 DB 表数据编码为 json。所以我使用了 json_encode()。

I use the tables given here - http://www.geekality.net/2011/08/21/country-names-continent-names-and-iso-3166-codes-for-mysql/

我使用这里给出的表格 - http://www.geekality.net/2011/08/21/country-names-continent-names-and-iso-3166-codes-for-mysql/

The behavious of this code seems to be different for different inputs.

对于不同的输入,此代码的行为似乎有所不同。

The query - $query = "SELECT * FROM countries ";doesn't return any json values.

The query -$query = "SELECT * FROM countries where continent_code='AS'";returns json values correctly.

whereas,$query = "SELECT * FROM countries where continent_code='EU'";also does't return anything.

查询 -$query = "SELECT * FROM countries ";不返回任何 json 值。

查询 -$query = "SELECT * FROM countries where continent_code='AS'";正确返回 json 值。

而,$query = "SELECT * FROM countries where continent_code='EU'";也不返回任何东西。

Similarily 'NA','AF' did not work and others work perfect.

类似地,'NA'、'AF' 不起作用,而其他的工作完美。

I'm weird of this behaviour of PHP's json_encode.

我对 PHP 的 json_encode 的这种行为感到奇怪。

This is my code.

这是我的代码。

<?php
$con=mysqli_connect('localhost','xxxx','xxxxx','joomla30');
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * FROM countries where continent_code='EU'") or die (mysqli_error($con));

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
      $orders[] = array(
          'CountryCode' => $row['code'],
          'CountryName' => $row['name']
          );
  }
//print_r($orders);

echo json_encode($orders);

mysqli_close($con);
?>

Until the previous line of json_encode, the datas are carried. So I think its json encode problem.

直到json_encode的前一行,数据才被携带。所以我认为它的json编码问题。

I tried to know about it using print_r($orders);.
I got the following output from it.

我试图使用print_r($orders);.
我从中得到了以下输出。

If I try for 'EU', i get this array.

如果我尝试 'EU',我会得到这个数组。

Array ( [0] => Array ( [CountryCode] => AD [CountryName] => Andorra ) 
[1] => Array ( [CountryCode] => AL [CountryName] => Albania ) 
[2] => Array ( [CountryCode] => AT [CountryName] => Austria ) 
[3] => Array ( [CountryCode] => AX [CountryName] => ?land Islands )...

If I try for 'AS', i get this array.

如果我尝试'AS',我会得到这个数组。

Array ( [0] => Array ( [CountryCode] => AE [CountryName] => United Arab Emirates ) 
[1] => Array ( [CountryCode] => AF [CountryName] => Afghanistan) 
[2] => Array ( [CountryCode] => AM [CountryName] => Armenia) 
[3] => Array ( [CountryCode] => AZ [CountryName] => Azerbaijan)...

Both the arrays looks alike right... But Json_encode works only on 'AS' and not for 'EU'.

两个数组看起来都一样……但是 Json_encode 仅适用于“AS”而不适用于“EU”。

Does anyone know how to solve this problem ? If so, pls tell me..

有谁知道如何解决这个问题?如果是这样,请告诉我..

回答by Wayne Whitty

You should make sure that every component of your web application uses UTF-8. This answerfrom another question will tell you how to do this. If you read thisfrom RFC4627:

您应该确保 Web 应用程序的每个组件都使用 UTF-8。这个来自另一个问题的答案将告诉你如何做到这一点。如果您从 RFC4627 中阅读此内容:

Encoding: JSON text SHALL be encoded in Unicode. The default encoding is UTF-8.

编码:JSON 文本应以 Unicode 编码。默认编码为 UTF-8。

The json_encode function requires all incoming data to be UTF-8 encoded. That's why strings such as ?land Islandsare probably causing you problems.

json_encode 函数要求所有传入的数据都采用 UTF-8 编码。这就是为什么诸如此类的字符串?land Islands可能会给您带来问题的原因。

回答by deceze

Just a random guess here: json_encoderequires all data you feed in to be UTF-8 encodedand it will fail otherwise. These countries where it fails, you likely have data which contains non-ASCII characters. Pure ASCII happens to be compatible with UTF-8, non-ASCII characters you need to take special care of. See UTF-8 all the way throughfor how to get UTF-8 encoded data out of your database (use mysqli_set_charset).

这里只是一个随机猜测:json_encode要求您输入的所有数据都是UTF-8 编码的,否则它将失败。这些失败的国家/地区,您可能拥有包含非 ASCII 字符的数据。纯 ASCII 恰好与 UTF-8 兼容,非 ASCII 字符需要特别注意。有关如何从数据库中获取 UTF-8 编码数据(使用),请一直查看UTF-8mysqli_set_charset