Create Multidimencional Array Dynamically in PHP

Asked

Viewed 55 times

0

Perhaps that question has already been asked but I have found nothing to help me. I believe that this doubt is simple but since I am no expert in PHP, I could not solve it.

Following:

In the company where I work, we have an ERP where the company’s sales orders are registered. By decision of the board, we will use an online ERP focused for Billing/Financial, but need to export (via their API) sales orders generated in our system.

What I’m not getting is to generate a array multidimensional with the portions of each order (I’m sure it’s a construction problem but haven’t solved yet).

The following is an example of what the syntax would look like if it were fixed data:

$chave->lista_parcelas = array(
    "parcela" => array(
        array(
            "data_vencimento" => "30/11/2019",
            "numero_parcela" => "001",
            "valor" => 10.50
        ),
        array(
            "data_vencimento" => "05/12/2019",
            "numero_parcela" => "002",
            "valor" => 10.49
        ),
        array(
            "data_vencimento" => "10/12/2019",
            "numero_parcela" => "003",
            "valor" => 10.49
        )
    )
);

Next, the way I tried to put this together (remembering that the information comes from a database):

$string_parc = "array('parcela' => array(";
$qt_parc = 0;
$string_PARC = "select * from VENPEDIDOSPAR ";
$string_PARC .= "where ";
$string_PARC .= "EMP_COD = ".$emp_cod." and ";
$string_PARC .= "PED_NUM = ".$ped_num;
$query_PARC = ibase_query($connection, $string_PARC);
while ($row_PARC = ibase_fetch_object($query_PARC)) {
    $qt_parc++;

    $vencto = new DateTime($row_PARC->VENCIMENTO);
    $vencto = date_format($vencto, "d/m/Y");

    $string_parc .= (($qt_parc == 1) ? "" : ",");
    $string_parc[] = "array('data_vencimento' => '$vencto',
                                'numero_parcela' => $qt_parc,
                                'valor' => $row_PARC->VALOR)";
}
$string_parc .= "))";

$chave->lista_parcelas = $string_parc;

There is no error but the information is not recorded. The support staff said that the code is entering as a string in the installments part (I also think), but I can’t do it the right way.

Can someone help me ?

  • It would look something like this https://repl.it/repls/StaidRaggedSandbox

  • Marcos, I made some changes to read from the table and it worked perfectly. Thank you. It helped a lot.

No answers

Browser other questions tagged

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