Reputation: 3692
I need pass on one json one element how an array of elemnts
Correct 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
}
Well, in my code before json_encode try several ways, but all times get incorrect 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
}
In my php code and try others but get same problem:
$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);
}
Edited:
After process all ellemnts I execute:
$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)));
Upvotes: 0
Views: 139
Reputation:
'invoice_lines'=> array($invoiceLines),
'charges' => array($paymentLines),
This is incorrect -- it's wrapping $invoiceLines
and $paymentLines
(which are already arrays) in an extra array each.
What you want is simply:
'invoice_lines' => $invoiceLines,
'charges' => $paymentLines,
Upvotes: 2