abkrim
abkrim

Reputation: 3692

How pass an array how array of jsons on PHP

For me is very complex to explain in english, and I'm looking for this problem without results.

Scenario I need pass a array to convert onto json. On this json thre're two values that's need a array of json, invoice_lines & charges

{
    "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
    }]
}

Well, when using code below get error at sending to API, becasue result is not an "array fo jsons"

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
]

convert to 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}"

My code

$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
);

Upvotes: 0

Views: 45

Answers (1)

u_mulder
u_mulder

Reputation: 54831

Add another array as a wrapper to your original array:

$invoiceLines = array(array( /* Your data here */ ));

Or in result data:

$invoiceLines = array( /* Your data here */ );

$newInvoice = array(
    'issued' => true,
    'customer' => 443183,
    'invoice_lines'=> array($invoiceLines),
    'charges' => array($invoiceCharges),
    'date' => $this->lastDate,
    'number' => $this->nextInvoice
);

Upvotes: 2

Related Questions