gpt4 book ai didi

php - json_encode,[括号]中需要的关联数组

转载 作者:行者123 更新时间:2023-12-04 15:53:37 26 4
gpt4 key购买 nike

我正在使用 PHP,为了使用 Curl 将一些数据提供给 API,我需要格式化一些字符串并且一直在使用 json_encode。它适用于更简单的位,但我无法弄清楚:

API 期望这样:

{
"id": "string",
"startTime": "2017-04-18T08:04:23.167Z",
"endTime": "2017-04-18T08:04:23.167Z",
"contacts": [
{
"id": "string",
"displayName": "string"
}

到目前为止我的代码:

$data_set_pre = array(
"id" => "",
"startTime" => "2017-04-14T07:47:59.028Z",
"endTime" => "2017-04-15T07:47:59.028Z",
"contacts" => array("id" => "ahashofsomenumbersandletters", "displayName" => "John Doe"),
);
$data_set = json_encode($data_set_pre);

不幸的是,这会产生:

{"id":"","startTime":"2017-04-14T07:47:59.028Z","endTime":"2017-04-15T07:47:59.028Z","contacts":{"id":"ahashofsomenumbersandletters","displayName":"John Doe"}}

我一直在研究,我得到的印象是 json_encode 将对所有内容进行编码,但一个从 0 开始的完整顺序数组作为对象,我现在不知道如何解决这个问题。(不,我无法更改 API 的要求,它由第三方提供)。

最佳答案

<?php
$data_set_pre = array(
"id" => "12",
"startTime" => "2017-04-14T07:47:59.028Z",
"endTime" => "2017-04-15T07:47:59.028Z",
"contacts" => array(
array(
"id" => "ahashofsomenumbersandletters", "displayName" => "John Doe"
)
)
);
$data_set = json_encode($data_set_pre);

关于php - json_encode,[括号]中需要的关联数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43468055/

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