【发布时间】:2020-02-27 13:01:13
【问题描述】:
尝试基于我尝试连接的 API 来实现这种 jsondata 结构:
{
"VoucherDate": "2019-10-27",
"VoucherText": "string",
"VoucherType": 2,
"Rows": [{
"AccountNumber": 0,
"DebitAmount": 0,
"CreditAmount": 0,
"TransactionText": "string"
},
{
"AccountNumber": 0,
"DebitAmount": 0,
"CreditAmount": 0,
"TransactionText": "string"
}
]
}
到目前为止,我在下面试验过的代码生成了这个结构。 Rows 数组没有得到和上面一样的结构,我哪里做错了?
{
"VoucherDate": "2019-10-31",
"VoucherText": "string",
"VoucherType": 2,
"Rows": [{
"TransactionText": "test"
}, {
"AccountNumber": 1000
}, {
"DebitAmount": 1
}, {
"CreditAMount": 2
}, {
"TransactionText": "test"
}, {
"AccountNumber": 1000
}, {
"DebitAmount": 1
}, {
"CreditAMount": 2
}]
}
php代码:
$postdata = array();
$postdata["VoucherDate"] = "2019-10-31";
$postdata["VoucherText"] = "string";
$postdata["VoucherType"] = 2;
$postdata["Rows"] = array();
for ($x = 1; $x <= 10; $x++) {
$postdata["Rows"][]["TransactionText"] = "string";
$postdata["Rows"][]["AccountNumber"] = 0;
$postdata["Rows"][]["DebitAmount"] = 0;
$postdata["Rows"][]["CreditAMount"] = ;
}
echo json_encode($postdata);
【问题讨论】:
-
每次调用
arr[]时,都是在创建一个新元素。您应该将它们全部添加到一个数组中,然后按照下面的@NigelRen 建议为其创建 1 个新元素。 -
本页没有点赞,所以不能加入骗子名单:stackoverflow.com/q/51354958/2943403