Mount Array to API

Asked

Viewed 88 times

0

Good morning to you all. I believe my question is primary, but I’m getting a lot of heat. Here in the company we will use an online ERP in which we intend to include our sales orders (using PHP) via Apis of this ERP. One of the parts of the Order Inclusion API deals with payment tranches and, according to their examples, the structure for interaction is as follows:

"lista_parcelas": {
"parcela": [
  {
    "data_vencimento": "02/10/2019",
    "numero_parcela": 1,
    "percentual": 50,
    "valor": 100
  },
  {
    "data_vencimento": "28/03/2020",
    "numero_parcela": 2,
    "percentual": 50,
    "valor": 100
  }
]

},

I tried to mount the array so:

$chave->lista_parcelas = array("parcela" => array("data_vencimento" => "31/10/2019",
						"numero_parcela" => 1,
						"percentual" => 40,
						"valor" => 120),
				"parcela" => array("data_vencimento" => "05/11/2019",
						"numero_parcela" => 2,
						"percentual" => 60,
						"valor" => 180));

But when I checked the result, only the second installment was recorded.

So I tried like this:

$chave->lista_parcelas = array("parcela" => array("data_vencimento" => "31/10/2019",
						"numero_parcela" => 1,
						"percentual" => 40,
						"valor" => 120),
				array("data_vencimento" => "05/11/2019",
						"numero_parcela" => 2,
						"percentual" => 60,
						"valor" => 180));

Thus, it gives error saying that some information is not part of the structure.

I believe it is my programming mistake. Could someone give me a light on how to correctly build this example ? Thank you in advance.

1 answer

-1

$a = array(

    "parcela" => array(

        array("data_vencimento" => "31/10/2019",
            "numero_parcela" => 1,
            "percentual" => 40,
            "valor" => 120),

        array("data_vencimento" => "05/11/2019",
            "numero_parcela" => 2,
            "percentual" => 60,
            "valor" => 180)

    )


);

$chave->lista_parcelas = json_encode($a);
  • Thanks for the reply, but despite creating the request, the routine gives the following error: Warning: Creating default Object from Empty value in .... in the $a = array line...

  • I got it. Thanks for your help.

  • @I know how you solved?

  • I took json_encode. My routine does this method later.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.