Getting information from an array within a foreach - PHP

Asked

Viewed 803 times

5

Good afternoon.

I’m developing a PHP application using Laravel, and I’m having a hard time.

I have a table with the Account name, which has the code of this account, represented by the variable $client->Code, also has the client’s CNPJ and the social reason represented by the variable $client->Clienterazao.

I created an array of CPNJ’s of clients. I am giving a select in the table where the information of these clients is found, and according to the cnpj of the array, I want to take the information of this client to insert it in my Insert.

Currently my Insert is working, but the variable $client->Code is being entered the value of 1 for all clients (each client has a unique code) and the $client->Clienterazao comes nothing. I’m doing something wrong, but I don’t know what it is.

Link to the cute code image in the visual code for better viewing: Link Imgur

OBS: the variable of $data->Nfse->InfNfse->TomadorServico->IdentificacaoTomador->CpfCnpj->Cnpj which is in if, is the CNPJ field coming from a JSON file.

Follows the code:

//select na tabela Conta, no qual consta todas as informações dos clientes
(cnpj, código, razão social etc..)

$pesquisaClientes = DB::connection('sqlsrv2')->select('SELECT * FROM 
ConsultaCnpj.dbo.Conta');

    // array dos CNPJ's dos clientes
    $CnpjClientes = array("43185230000185", "43037969000140", "52391703000191", "00697722000147", "35259696000150", "14189856000161", "11684590000135",
    "00299388000173", "12515276000191", "73800260000115", "26562892000108", "04711149000130", "11888864000108", "24193260000199", "34482091000160", 
    "10638562000119", "15615776000193", "22165071000187", "02505297000172", "03662446000170", "07863214000130", "34639419000100", "26619841000175", 
    "13738204000176", "62021837000174", "24417008000116", "11005444000136", "08303182000181", "84318799000159", "04768671000158", "00593411000138", 
    "06572788000197", "03875295000138", "08332733000135", "00237222000122", "06272868000127", "08336841000186", "04734406000159", "07647181000191", 
    "04769874000169", "08506339000176", "27149095000166", "34476101000155", "84042423000164", "13161344000124", "26753715000109", "23498256000176", 
    "00577473000156", "11425519000138", "90601147000120", "08077490000136", "04185220000198", "01646861000104", "02116365000101", "05379164000195", 
    "56319882000107", "15678394000109", "00581009000133", "08641589000119", "34918342000107", "11674272000193", "07801011000110", "30892350000170", 
    "87070843000142", "10456659000100", "44407989000128", "10338929000189", "06186786000160", "11674751000100", "08299638000187", "06274668000103", 
    "11578277000112", "73603748000152", "73392409000174", "05644027000130", "56321573000171", "60993482000150", "00085803000196", "87096616000196", 
    "57352635000175", "33658204000173");

    foreach ($pesquisaClientes as $cliente) {

            if (in_array($data->Nfse->InfNfse->TomadorServico->IdentificacaoTomador->CpfCnpj->Cnpj, $CnpjClientes)) {

                //insert conta de debito (cliente)
                DB::insert('insert into LancaContaContabil (Codigo, DataOper, DespRateio, Valor, Receita, AgenteCobrador, 
                DespHistorico, Complemento, Orcado, Encerrado, CodigoContaContabil, LoteLanca,
                Rascunho, DC) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)', 
                array($ult+1, $data->Nfse->InfNfse->Competencia, NULL, $data->Nfse->InfNfse->Servico->Valores->ValorServicos,
                0, NULL, 9182, $numero_nota . ', ' . $cliente->ClienteRazao, 0, 0, $cliente->Codigo, $lanca, 0, 'D'));

            }
  • shares the output of the variable $cliente

  • So 13dev, right? The output of the variable $client->Clienterazao is null and the variable $client->Code is always 1

  • 1

    shows the output of the ex object: print_r($cliente);

  • The output of $customer is this: stdClass Object ( [Numero] => 1.0.0.00.00 [Denominacao] => ATIVO [NumeroReduzido] => 17 [Codigo] => 1 [Cnpj] => [ClienteRazao] => ) This information is from the first record of my table, something is wrong, he was supposed to get this information according to the CNPJ that is in the array and table. But my array of clients' CNPJ is not being taken into account

  • I think the error will be in the variable $pesquisaClientes, containing only one record, check this

  • opa, this test I had already done... ta everything ok with the $researchClientes, is coming to my whole table, without any problem

  • solved the problem! I was giving the wrong foreach, it was to iterate on the array of my tax bills that were coming from a json. Thanks for the help!

  • 1

    If you solved the problem post a reply

  • Post an answer fjurr, this helps the community to grow, plus your question will come out of the category 'No answers'.

  • There are 3 things that don’t make sense to me, you’re going through all the data in the database, to enter only 1 record, if it’s in your cnpj array, and then you’re creating a numerical ID without query, which makes no sense at all: $ult+1, what connects cnpj to query data? What is inside that: $pesquisaClientes?

  • You’re wearing a collection $pesquisaClientes, with an alias $cliente and making the reference as $data ?

Show 6 more comments

1 answer

-2

Man! So if you’re using Laravel, semantically speaking, the best way to do this would be to create a Model and a client Migration, so you call a get() or find() method of the client class with the Where('cnpj','=','38748578000137' method) and you will get all the customer data you need.

Browser other questions tagged

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