对我而言,用英语解释是非常复杂的,我在没有结果的情况下寻找这个问题。
情景 我需要传递一个数组转换为json。 在这个json上,有两个值需要一个json数组, invoice_lines&电荷
{
"description": "<ADD STRING VALUE>",
"annotations": "<ADD STRING VALUE>",
"date": "<ADD STRING VALUE>",
"serie": "<ADD STRING VALUE>",
"issued": false,
"number": 0,
"customer": 0,
"footer": "Configuración del usuario.",
"irm": "Configuración del usuario.",
"invoice_lines": [{
"quantity": 0,
"concept": "<ADD STRING VALUE>",
"amount": 0,
"discount": 0,
"tax": 0,
"surcharge": 0,
"retention": 0
}],
"charges": [{
"date": "<ADD STRING VALUE>",
"amount": 0,
"method": "cash",
"destination_account": 0,
"origin_account": "<ADD STRING VALUE>",
"charged": false
}]
}
好吧,当使用下面的代码时,在发送到API时会出错,因为结果不是&#34;数组&#34;
array:6 [
"issued" => true
"customer" => 443183
"invoice_lines" => array:6 [
"quantity" => 1
"concept" => "Factura no emitida por error informático"
"amount" => 1
"discount" => 0
"tax" => 0
"retention" => 0
]
"charges" => array:5 [
"date" => "2018-03-04"
"amount" => 1
"method" => "cash"
"destination_account" => 443183
"charged" => true
]
"date" => "2018-03-04"
"number" => 1
]
转换为json show:
"{"issued":true,"customer":443183,"invoice_lines":{"quantity":1,"concept":"Factura no emitida por error inform\u00e1tico","amount":1,"discount":0,"tax":0,"retention":0},"charges":{"date":"2018-03-04","amount":1,"method":"cash","destination_account":443183,"charged":true},"date":"2018-03-04","number":1}"
我的代码
$invoiceLines = array(
'quantity' => 1,
'concept' => 'Factura no emitida por error informático',
'amount' => 1,
'discount' => 0,
'tax' => 0,
'retention' => 0
);
$invoiceCharges = array(
'date' => $this->lastDate,
'amount' => 1,
'method' => 'cash',
'destination_account' => 443183,
'charged' => true
);
$newInvoice = array(
'issued' => true,
'customer' => 443183,
'invoice_lines'=> $invoiceLines,
'charges' => $invoiceCharges,
'date' => $this->lastDate,
'number' => $this->nextInvoice
);
答案 0 :(得分:2)
将另一个数组添加为原始数组的包装器:
$invoiceLines = array(array( /* Your data here */ ));
结果数据中的或:
$invoiceLines = array( /* Your data here */ );
$newInvoice = array(
'issued' => true,
'customer' => 443183,
'invoice_lines'=> array($invoiceLines),
'charges' => array($invoiceCharges),
'date' => $this->lastDate,
'number' => $this->nextInvoice
);