使用json_encode()函数的PHP数组中的JSON键值对 [英] JSON Key Value Pairs within an Array in PHP using the json_encode() Function
本文介绍了使用json_encode()函数的PHP数组中的JSON键值对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试以特定语法获取JSON输出
I am trying to get my JSON output in a particular syntax
这是我的代码:
$ss = array('1.jpg', '2.jpg');
$dates = array('eu' => '59.99', 'us' => '39.99');
$array1 = array('name' => 'game1', 'publisher' => 'ubisoft', 'screenshots' => $ss, 'dates' => $dates, 'added' => '2014/12/31');
echo json_encode($array1);
它给了我这个输出:
{
name: "game1",
publisher: "ubisoft",
screenshots: [
"1.jpg",
"2.jpg"
],
dates: {
eu: "59.99",
us: "39.99"
},
],
added: "2014/12/31"
}
这是关闭的,但不完全是我需要的.日期的格式需要略有不同.像这样:
which is CLOSE but, not exactly what I need. The dates need to be formatted slightly different. Like this:
{
name: "game1",
publisher: "ubisoft",
screenshots: [
"1.jpg",
"2.jpg"
],
dates: [
{
eu: "59.99"
},
{
us: "39.99"
}
],
added: "2014/12/31"
}
我尝试为$ dates数组添加更多维度,但这仍然无法提供正确的输出.不幸的是,json_encode()函数的php手册并没有提供太多帮助,我在Google搜索中发现的关于php内更复杂的json编码的文档也很少.
I have tried adding more dimensions to the $dates array, but that still doesn't give quite the right output. Unfortunately the php manual for the json_encode() function doesn't provide much help and there is very little documentation on more complex json encodes within php that i have found with google searching.
任何帮助将不胜感激.
推荐答案
将$dates
分配更改为:
$dates = array(array('eu' => '59.99'), array('us' => '39.99'));
这篇关于使用json_encode()函数的PHP数组中的JSON键值对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文