简体   繁体   English

json_encode中的数组数组

[英]Array of arrays in json_encode

I need pass on one json one element how an array of elemnts 我需要传递一个json一个元素如何将元素数组

Correct 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
}

Well, in my code before json_encode try several ways, but all times get incorrect json 好吧,在我的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
}

In my php code and try others but get same problem: 在我的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);

}

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)));
'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. 这是不正确-它包裹$invoiceLines$paymentLines在每一个额外的数组(已阵列)。

What you want is simply: 您想要的只是:

'invoice_lines' => $invoiceLines,
'charges' => $paymentLines,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM