gpt4 book ai didi

php - PHP 数组中的 JSON 键值对使用 json_encode() 函数

转载 作者:搜寻专家 更新时间:2023-10-31 20:39:23 26 4
gpt4 key购买 nike

我正在尝试以特定语法获取我的 JSON 输出

这是我的代码:

$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"
}

这是 CLOSE,但不完全是我需要的。日期的格式需要略有不同。像这样:

{
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 编码的文档非常少。

如有任何帮助,我们将不胜感激。

最佳答案

$dates 赋值更改为:

$dates = array(array('eu' => '59.99'), array('us' => '39.99'));

关于php - PHP 数组中的 JSON 键值对使用 json_encode() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26941090/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com