1

I have a foreach that checks multiple requests via API and selects only those requests that have items with the prefix of sku == "MCA". However, as I've done below, I can only put a single order in the array, when I'd have to place all the requests.

foreach($listaPedidos as $pedido) :
    $i = 0;
    $somatorio = array();

    $resulta = $client->salesOrderInfo($session, $pedido->increment_id);
    foreach($resulta->items as $item) :
        $sku = $item->sku;
        if (substr($sku, 0,3) == "MCA") :
            $email = $pedido->customer_email;
            $codCli = idClienteMca($conn, $session, $client, $email);
            $dataCadastro = date("Y-m-d H:i:s");
            $qtdItens = ++$i;
            $somatorio[] = (float)$item->row_total;
            $totalPedido = array_sum($somatorio);
            $observacao = "";
            $boleto = "0";
            $transportadora = "";

            $todosPedidos = array(
                "codcli"        => $codCli,
                "datacadastro"  => $dataCadastro,
                "qtditens"      => $qtdItens,
                "totalpedido"   => $totalPedido,
                "observacao"    => $observacao,
                "boleto"        => $boleto
            );

        endif;
    endforeach;

endforeach;

The result I expected would be this:

array(2) {
  array(6) {
    ["codcli"]=>
    string(3) "576"
    ["datacadastro"]=>
    string(19) "2017-12-28 17:22:24"
    ["qtditens"]=>
    int(3)
    ["totalpedido"]=>
    float(74.19)
    ["observacao"]=>
    string(0) ""
    ["boleto"]=>
    string(1) "0"
  }
  array(6) {
    ["codcli"]=>
    string(3) "890"
    ["datacadastro"]=>
    string(19) "2017-12-28 17:55:24"
    ["qtditens"]=>
    int(4)
    ["totalpedido"]=>
    float(154.89)
    ["observacao"]=>
    string(0) ""
    ["boleto"]=>
    string(1) "0"
  }
}

How do I bring all the requests, not just the last iteration?

Tks!

4
  • English language pls Commented Dec 28, 2017 at 20:06
  • 1
    @tadman excuse me. Changed for english Commented Dec 28, 2017 at 21:04
  • One thing to note here is the use of if ... : and endif is fairly non-standard. While valid, most code bases use the conventional if { ... } structure instead if only because it's less verbose. Commented Dec 28, 2017 at 21:08
  • Tks for the tip! Commented Dec 28, 2017 at 21:16

1 Answer 1

3

Problem You are over-writing $todosPedidos variable inside foreach() again and again, and that's why you got only the last result.

Solution:-

  1. define $todosPedidos = []; before first foreach().

  2. Change $todosPedidos to $todosPedidos[]

So code needs to be:-

$todosPedidos = [];
foreach($listaPedidos as $pedido) :
    $i = 0;
    $somatorio = array();

    $resulta = $client->salesOrderInfo($session, $pedido->increment_id);
    foreach($resulta->items as $item) :
        $sku = $item->sku;
        if (substr($sku, 0,3) == "MCA") :
            $email = $pedido->customer_email;
            $codCli = idClienteMca($conn, $session, $client, $email);
            $dataCadastro = date("Y-m-d H:i:s");
            $qtdItens = ++$i;
            $somatorio[] = (float)$item->row_total;
            $totalPedido = array_sum($somatorio);
            $observacao = "";
            $boleto = "0";
            $transportadora = "";

            $todosPedidos[] = array(
                "codcli"        => $codCli,
                "datacadastro"  => $dataCadastro,
                "qtditens"      => $qtdItens,
                "totalpedido"   => $totalPedido,
                "observacao"    => $observacao,
                "boleto"        => $boleto
            );

        endif;
    endforeach;

endforeach;
print_r($todosPedidos); // to check that all data coming or not?
Sign up to request clarification or add additional context in comments.

1 Comment

But the result was not yet expected. Now the answer was 6 arrays and I expected only 2. I have 2 equal orders in the database and I need to write in the array mainly the total of items and also the total value of the request. But that way the result was a loop for each product. I need a loop per order.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.