[英]Array of arrays in json_encode
我需要传递一个json一个元素如何将元素数组
正确的json:
{
"issued": true,
"customer": 451071,
"invoice_lines": [{
"quantity": 1,
"concept": "Renovar Dominio - xxx.org - 1 A\u00f1o(s) (22\/03\/2018 - 21\/03\/2019) + Protecci\u00f3n de ID",
"amount": 13.15,
"discount": 0,
"tax": 21,
"retention": 0
}, {
"quantity": 1,
"concept": "Renovar Dominio - xxx.net - 1 A\u00f1o(s) (22\/03\/2018 - 21\/03\/2019) + Protecci\u00f3n de ID",
"amount": 12.73,
"discount": 0,
"tax": 21,
"retention": 0
}],
"charges": [{
"date": "2018-03-05",
"amount": 74.69,
"method": "paypal",
"destination_account": 39720,
"charged": true
}],
"date": "2018-03-05",
"number": 4
}
好吧,在我的json_encode之前的代码中尝试了几种方法,但是始终会得到不正确的json
{
"issued": true,
"customer": 451071,
"invoice_lines": [
[{
"quantity": 1,
"concept": "Renovar Dominio - xxx.org - 1 A\u00f1o(s) (22\/03\/2018 - 21\/03\/2019) + Protecci\u00f3n de ID",
"amount": 13.15,
"discount": 0,
"tax": 21,
"retention": 0
}, {
"quantity": 1,
"concept": "Renovar Dominio - xxxx.net - 1 A\u00f1o(s) (22\/03\/2018 - 21\/03\/2019) + Protecci\u00f3n de ID",
"amount": 12.73,
"discount": 0,
"tax": 21,
"retention": 0
}]
],
"charges": [{
"date": "2018-03-05",
"amount": 74.69,
"method": "paypal",
"destination_account": 39720,
"charged": true
}],
"date": "2018-03-05",
"number": 4
}
在我的php代码中,尝试其他操作,但遇到相同的问题:
$invoiceLines = array();
foreach ($whmcsLines as $whmcsLine) {
$lines = [
'quantity' => 1,
'concept' => str_replace("\n","",$whmcsLine->description),
'amount' => (double)$whmcsLine->amount,
'discount' => 0,
'tax' => $whmcsLine->taxed ? 21 : 0,
'retention' => 0
];
array_push($invoiceLines, $line);
}
编辑:
处理完所有提示后,我执行:
$newInvoice = array(
'issued' => true,
'customer' => $this->customerId,
'invoice_lines'=> array($invoiceLines),
'charges' => array($paymentLines),
'date' => $this->lastDate,
'number' => $this->nextInvoice
);
$this->someAction(json_encode($newInvoice)));
'invoice_lines'=> array($invoiceLines),
'charges' => array($paymentLines),
这是不正确-它包裹$invoiceLines
和$paymentLines
在每一个额外的数组(已阵列)。
您想要的只是:
'invoice_lines' => $invoiceLines,
'charges' => $paymentLines,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.