PHP json_encode无法部分处理数组 [英] php json_encode not working on arrays partially

查看:251
本文介绍了PHP json_encode无法部分处理数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

I use the tables given here - 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.

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

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

$query = "SELECT * FROM countries where continent_code='EU'";也不会返回任何内容.

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.

类似"NA","AF"无效,而其他人则完美.

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

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

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

这是我的代码.

<?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);
?>

直到json_encode的上一行,数据都被携带.所以我认为它是json编码问题.

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

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

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

如果我尝试使用"EU",则会得到该数组.

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

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 )...

如果我尝试使用"AS",则会得到该数组.

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

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)...

这两个数组看起来都很相似... 但是Json_encode仅在'AS'上有效,而在'EU'上无效.

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

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

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

推荐答案

您应确保Web应用程序的每个组件都使用UTF-8.从另一个问题中此答案将告诉您如何执行此操作.如果您从RFC4627阅读此:

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

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

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

json_encode函数要求所有传入数据均采用UTF-8编码.这就是为什么诸如Åland Islands之类的字符串可能会导致您出现问题的原因.

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

这篇关于PHP json_encode无法部分处理数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆