如何为json编码编写数组看起来像这样

时间:2013-02-16 03:25:33

标签: php json

我正在尝试获取特定的json响应,但响应中的一个数组作为对象传递。

"countries":{"TW":8,"JP":5,"AU":6,"MX":12,"CL":4,"HK":2,"US":14,"AR":4,"ES":1,"BR":1,"MY":9,"IT":12,"DE":1,"GB":1,"PE":6,"TR":1,"KR":3,"IE":1,"CA":2,"FR":1,"VE":2,"IL":1,"PT":1,"NL":1,"PL":1}

但我需要它看起来像这样:

"countries":[["Brazil", 40.5],["US", 30],["Canada", 19.5], ["England", 10]]

如何在PHP中为json_encode响应构建该数组?

现在我有:

$countries['US']=14;
$countries['CL']=4;
....

然后我将该数组($ countries)添加到$ data数组中,这是一个json编码的

$data['countries'] = $countries;

这给出了我首先发布的结果。但我需要的是第二种格式。

任何人都知道我错过了什么?

谢谢!

1 个答案:

答案 0 :(得分:1)

$countries = Array();
$countries[] = Array('Brazil', 40.5);
 ...