Problem with PHP FOREACH

Asked

Viewed 54 times

1

My problem is that my foreach only works when returning in XML, comes with more than one value in the array, but when it comes only 1, it doesn’t work.

The return in XML when comes more than 1 value would be like this:

<NewDataSet>
  <HSONLINE>
      <CONSIGNACAO id="1">
      ...Valores do array...
      </CONSIGNACAO>
      <CONSIGNACAO id="2">
      ...Valores do array...
      </CONSIGNACAO>
  </HSONLINE
</NewDataSet>

And when XML comes with only 1 value it would be like this:

<NewDataSet>
  <HSONLINE>
      <CONSIGNACAO id="1">
      ...Valores do array...
      </CONSIGNACAO>
  </HSONLINE
</NewDataSet>

This is my code in PHTML:

foreach($consignado as $registro){

//Verifico se a variavel não é um array
if(!is_array(current($retorno['HSONLINE']['CONSIGNACAO']))){
    $registro = $retorno['HSONLINE']['CONSIGNACAO'];
}

//Declado as variaveis
$nomeBanco = $registro['NOME_BANCO'];
$dataInicio = $registro['DATA_INICIO'];
$parcelaTotal = $registro['PARCELA_TOTAL'];
$dataTermino = $registro['DATA_TERMINO'];
$exclusaoBanco = $registro['EXCLUIDO_BANCO'];
$valorEmprestado = $registro['VALOR_EMPRESTIMO'];
$valorParcela = $registro['VALOR_PARCELA'];

$contrato = $registro['CONTRATO'];
$fimDesconto = $registro['FIM_DESCONTO'];
$excluidoAPS = $registro['EXCLUIDO_APS'];
$codigoBanco = $registro['CODIGO_BANCO'];
$situacao = $registro['SITUACAO'];
$dataConsignacao = $registro['DATA_CONSIGNACAO'];
$banco = $registro['BANCO'];


$tipo = $registro['TIPO'];
$tipoCodigo = $registro['TIPO_CODIGO'];
$competenciaDesconto = $registro['COMPETENCIA_DESCONTO'];
$tipoDesconto = $registro['TIPO_DESCONTO'];
$parcelasAberto = $registro['PARCELAS_ABERTO'];
$saldoAproximado = $registro['SALDO_APROXIMADO'];
$dataAverbacao = $registro['DATA_AVERBACAO'];




$pintar=" 
    <table class=\"table-custom\">      
        <tbody>
            <tr>
                                    <td class=\"banco2\">$nomeBanco</td>
                                    <td class=\"tdCustom2\">$dataInicio</td>
                                    <td class=\"tdCustom2\">$parcelaTotal</td>
                                    <td class=\"tdCustom2\">$dataTermino</td>
                                    <td class=\"tdCustom2\">$exclusaoBanco</td>
                                    <td class=\"tdCustom2\">$valorEmprestado</td>
                                    <td class=\"tdCustom2\">$valorParcela</td>
            </tr>
            <tr class=\"tr-esconder1\">
                                    <td class=\"contrato2\">$contrato</td>
                                    <td class=\"tdCustom2\">$fimDesconto</td>  
                                    <td class=\"tdCustom2\">$excluidoAPS</td>
                                    <td class=\"tdCustom2\">$codigoBanco</td>
                                    <td class=\"tdCustom2\">$situacao</td>
                                    <td class=\"tdCustom2\">$dataConsignacao</td> 
                                    <td class=\"tdCustom2\">$banco</td> 
            </tr>
            <tr class=\"tr-esconder1\">
                                    <td class=\"contrato2\">$tipo</td>
                                    <td class=\"tdCustom2\">$tipoCodigo</td>  
                                    <td class=\"tdCustom2\">$competenciaDesconto</td>
                                    <td class=\"tdCustom2\">$tipoDesconto</td>
                                    <td class=\"tdCustom2\">$parcelasAberto</td> 
                                    <td class=\"tdCustom2\">$saldoAproximado</td> 
                                    <td class=\"tdCustom2\">$dataAverbacao</td> 
            </tr>
            <br/>
        </tbody>
    </table>";

$pintar = str_replace(">Array<", ">Não Informado<", $pintar);
echo $pintar;

    //Aqui ele faz a verificação se não é um array e da o break
    if(!is_array(current($retorno['HSONLINE']['CONSIGNACAO']))){
        break;
    }
}

The page layout looks like this, when XML comes with more than 1 CONSIGNACAO Quando o retorno vem com mais de 1 informação

And it gets like this when only one comes: Quando o retorno vem com apenas 1 informação

Thanks for your help!

  • There’s no missing one > after the </HSONLINE?

  • This was my mistake, but in the XML that I use is right

1 answer

0

I took the liberty of making some changes to your code. Among them, I changed the access to the elements of the form $chave=>$valor for Object Operator ->. I also commented on both if links inside the foreach (this check does not seem relevant, since the foreach already performs them, or are not aware if any of the properties that will be accessed do not exist). In addition I created a small xml file (following the template you passed) to test the execution of the code. Then the file xml.xml was like this:

<?xml version="1.0" encoding="utf-8"?>
<NewDataSet>
  <HSONLINE>
    <CONSIGNACAO id="1">
        <NOME_BANCO>Campo 1</NOME_BANCO>
        <DATA_INICIO>Campo 2</DATA_INICIO>
        <PARCELA_TOTAL>Campo 3</PARCELA_TOTAL>
        <DATA_TERMINO>Campo 4</DATA_TERMINO>
        <EXCLUIDO_BANCO>Campo 5</EXCLUIDO_BANCO>
        <VALOR_EMPRESTIMO>Campo 6</VALOR_EMPRESTIMO>
        <VALOR_PARCELA>Campo 7</VALOR_PARCELA>

        <CONTRATO>Campo 8</CONTRATO>
        <FIM_DESCONTO>Campo 9</FIM_DESCONTO>
        <EXCLUIDO_APS>Campo 10</EXCLUIDO_APS>
        <CODIGO_BANCO>Campo 11</CODIGO_BANCO>
        <SITUACAO>Campo 12</SITUACAO>
        <DATA_CONSIGNACAO>Campo 13</DATA_CONSIGNACAO>
        <BANCO>Campo 14</BANCO>

        <TIPO>Campo 15</TIPO>
        <TIPO_CODIGO>Campo 15</TIPO_CODIGO>
        <COMPETENCIA_DESCONTO>Campo 15</COMPETENCIA_DESCONTO>
        <TIPO_DESCONTO>Campo 15</TIPO_DESCONTO>
        <PARCELAS_ABERTO>Campo 15</PARCELAS_ABERTO>
        <SALDO_APROXIMADO>Campo 15</SALDO_APROXIMADO>
        <DATA_AVERBACAO>Campo 15</DATA_AVERBACAO>

    </CONSIGNACAO>
    <CONSIGNACAO id="2">
        <NOME_BANCO>Campo 1</NOME_BANCO>
        <DATA_INICIO>Campo 2</DATA_INICIO>
        <PARCELA_TOTAL>Campo 3</PARCELA_TOTAL>
        <DATA_TERMINO>Campo 4</DATA_TERMINO>
        <EXCLUIDO_BANCO>Campo 5</EXCLUIDO_BANCO>
        <VALOR_EMPRESTIMO>Campo 6</VALOR_EMPRESTIMO>
        <VALOR_PARCELA>Campo 7</VALOR_PARCELA>

        <CONTRATO>Campo 8</CONTRATO>
        <FIM_DESCONTO>Campo 9</FIM_DESCONTO>
        <EXCLUIDO_APS>Campo 10</EXCLUIDO_APS>
        <CODIGO_BANCO>Campo 11</CODIGO_BANCO>
        <SITUACAO>Campo 12</SITUACAO>
        <DATA_CONSIGNACAO>Campo 13</DATA_CONSIGNACAO>
        <BANCO>Campo 14</BANCO>

        <TIPO>Campo 15</TIPO>
        <TIPO_CODIGO>Campo 15</TIPO_CODIGO>
        <COMPETENCIA_DESCONTO>Campo 15</COMPETENCIA_DESCONTO>
        <TIPO_DESCONTO>Campo 15</TIPO_DESCONTO>
        <PARCELAS_ABERTO>Campo 15</PARCELAS_ABERTO>
        <SALDO_APROXIMADO>Campo 15</SALDO_APROXIMADO>
        <DATA_AVERBACAO>Campo 15</DATA_AVERBACAO>
    </CONSIGNACAO>
  </HSONLINE>
</NewDataSet>

As can be seen the xml file has two CONSIGNACAO elements (so we gave to test with many elements and with only one element).

Already the PHP code looked like this:

<?php

$retorno = simplexml_load_file('xml.xml');

//retorna todos os elementos CONSIGNACAO
$consignado = $retorno->HSONLINE;

foreach($consignado->children() as $registro){
    //melhor seria checar se as propriedades que vão ser acessadas existem
    /*if(!is_array(current($retorno->HSONLINE->CONSIGNACAO))){
        $registro = $retorno->HSONLINE->CONSIGNACAO;
    }*/

    //Declado as variaveis
    $nomeBanco = $registro->NOME_BANCO;
    $dataInicio = $registro->DATA_INICIO;
    $parcelaTotal = $registro->PARCELA_TOTAL;
    $dataTermino = $registro->DATA_TERMINO;
    $exclusaoBanco = $registro->EXCLUIDO_BANCO;
    $valorEmprestado = $registro->VALOR_EMPRESTIMO;
    $valorParcela = $registro->VALOR_PARCELA;

    $contrato = $registro->CONTRATO;
    $fimDesconto = $registro->FIM_DESCONTO;
    $excluidoAPS = $registro->EXCLUIDO_APS;
    $codigoBanco = $registro->CODIGO_BANCO;
    $situacao = $registro->SITUACAO;
    $dataConsignacao = $registro->DATA_CONSIGNACAO;
    $banco = $registro->BANCO;


    $tipo = $registro->TIPO;
    $tipoCodigo = $registro->TIPO_CODIGO;
    $competenciaDesconto = $registro->COMPETENCIA_DESCONTO;
    $tipoDesconto = $registro->TIPO_DESCONTO;
    $parcelasAberto = $registro->PARCELAS_ABERTO;
    $saldoAproximado = $registro->SALDO_APROXIMADO;
    $dataAverbacao = $registro->DATA_AVERBACAO;

    $pintar=" 
    <table class=\"table-custom\">      
        <tbody>
            <tr>
                                    <td class=\"banco2\">$nomeBanco</td>
                                    <td class=\"tdCustom2\">$dataInicio</td>
                                    <td class=\"tdCustom2\">$parcelaTotal</td>
                                    <td class=\"tdCustom2\">$dataTermino</td>
                                    <td class=\"tdCustom2\">$exclusaoBanco</td>
                                    <td class=\"tdCustom2\">$valorEmprestado</td>
                                    <td class=\"tdCustom2\">$valorParcela</td>
            </tr>
            <tr class=\"tr-esconder1\">
                                    <td class=\"contrato2\">$contrato</td>
                                    <td class=\"tdCustom2\">$fimDesconto</td>  
                                    <td class=\"tdCustom2\">$excluidoAPS</td>
                                    <td class=\"tdCustom2\">$codigoBanco</td>
                                    <td class=\"tdCustom2\">$situacao</td>
                                    <td class=\"tdCustom2\">$dataConsignacao</td> 
                                    <td class=\"tdCustom2\">$banco</td> 
            </tr>
            <tr class=\"tr-esconder1\">
                                    <td class=\"contrato2\">$tipo</td>
                                    <td class=\"tdCustom2\">$tipoCodigo</td>  
                                    <td class=\"tdCustom2\">$competenciaDesconto</td>
                                    <td class=\"tdCustom2\">$tipoDesconto</td>
                                    <td class=\"tdCustom2\">$parcelasAberto</td> 
                                    <td class=\"tdCustom2\">$saldoAproximado</td> 
                                    <td class=\"tdCustom2\">$dataAverbacao</td> 
            </tr>
            <br/>
        </tbody>
    </table>";

    $pintar = str_replace(">Array<", ">Não Informado<", $pintar);
    echo $pintar;

    //o foreach verifica isso
    /*if(!is_array(current($retorno['HSONLINE']['CONSIGNACAO']))){
        break;
    }*/
}

When the xml file has several elements the following output is generated (as it does not have the css classes some lines will not be hidden):

 <table class="table-custom">      
        <tbody>
            <tr>
                                    <td class="banco2">Campo 1</td>
                                    <td class="tdCustom2">Campo 2</td>
                                    <td class="tdCustom2">Campo 3</td>
                                    <td class="tdCustom2">Campo 4</td>
                                    <td class="tdCustom2">Campo 5</td>
                                    <td class="tdCustom2">Campo 6</td>
                                    <td class="tdCustom2">Campo 7</td>
            </tr>
            <tr class="tr-esconder1">
                                    <td class="contrato2">Campo 8</td>
                                    <td class="tdCustom2">Campo 9</td>  
                                    <td class="tdCustom2">Campo 10</td>
                                    <td class="tdCustom2">Campo 11</td>
                                    <td class="tdCustom2">Campo 12</td>
                                    <td class="tdCustom2">Campo 13</td> 
                                    <td class="tdCustom2">Campo 14</td> 
            </tr>
            <tr class="tr-esconder1">
                                    <td class="contrato2">Campo 15</td>
                                    <td class="tdCustom2">Campo 15</td>  
                                    <td class="tdCustom2">Campo 15</td>
                                    <td class="tdCustom2">Campo 15</td>
                                    <td class="tdCustom2">Campo 15</td> 
                                    <td class="tdCustom2">Campo 15</td> 
                                    <td class="tdCustom2">Campo 15</td> 
            </tr>
            <br/>
        </tbody>
    </table> 
    <table class="table-custom">      
        <tbody>
            <tr>
                                    <td class="banco2">Campo 1</td>
                                    <td class="tdCustom2">Campo 2</td>
                                    <td class="tdCustom2">Campo 3</td>
                                    <td class="tdCustom2">Campo 4</td>
                                    <td class="tdCustom2">Campo 5</td>
                                    <td class="tdCustom2">Campo 6</td>
                                    <td class="tdCustom2">Campo 7</td>
            </tr>
            <tr class="tr-esconder1">
                                    <td class="contrato2">Campo 8</td>
                                    <td class="tdCustom2">Campo 9</td>  
                                    <td class="tdCustom2">Campo 10</td>
                                    <td class="tdCustom2">Campo 11</td>
                                    <td class="tdCustom2">Campo 12</td>
                                    <td class="tdCustom2">Campo 13</td> 
                                    <td class="tdCustom2">Campo 14</td> 
            </tr>
            <tr class="tr-esconder1">
                                    <td class="contrato2">Campo 15</td>
                                    <td class="tdCustom2">Campo 15</td>  
                                    <td class="tdCustom2">Campo 15</td>
                                    <td class="tdCustom2">Campo 15</td>
                                    <td class="tdCustom2">Campo 15</td> 
                                    <td class="tdCustom2">Campo 15</td> 
                                    <td class="tdCustom2">Campo 15</td> 
            </tr>
            <br/>
        </tbody>
    </table>

And with only one element in the xml file has the expected output as well:

<table class="table-custom">      
        <tbody>
            <tr>
                                    <td class="banco2">Campo 1</td>
                                    <td class="tdCustom2">Campo 2</td>
                                    <td class="tdCustom2">Campo 3</td>
                                    <td class="tdCustom2">Campo 4</td>
                                    <td class="tdCustom2">Campo 5</td>
                                    <td class="tdCustom2">Campo 6</td>
                                    <td class="tdCustom2">Campo 7</td>
            </tr>
            <tr class="tr-esconder1">
                                    <td class="contrato2">Campo 8</td>
                                    <td class="tdCustom2">Campo 9</td>  
                                    <td class="tdCustom2">Campo 10</td>
                                    <td class="tdCustom2">Campo 11</td>
                                    <td class="tdCustom2">Campo 12</td>
                                    <td class="tdCustom2">Campo 13</td> 
                                    <td class="tdCustom2">Campo 14</td> 
            </tr>
            <tr class="tr-esconder1">
                                    <td class="contrato2">Campo 15</td>
                                    <td class="tdCustom2">Campo 15</td>  
                                    <td class="tdCustom2">Campo 15</td>
                                    <td class="tdCustom2">Campo 15</td>
                                    <td class="tdCustom2">Campo 15</td> 
                                    <td class="tdCustom2">Campo 15</td> 
                                    <td class="tdCustom2">Campo 15</td> 
            </tr>
            <br/>
        </tbody>
    </table>

Browser other questions tagged

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