Problem with FOREACH PHP

1

My problem is that my foreach only works when in return in XML, it comes with more than one value in the array, but when it only comes 1, it does not work.

The XML return when more than 1 value comes in:

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

And when the XML comes with only 1 value it would look 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 CONSIGNATION

Andthisiswhenonlyonecomes:

Thanks in advance for the help!

    
asked by anonymous 03.11.2017 / 21:56

1 answer

0

I took the liberty of making some changes to your code. Among them, I changed access to the elements of the form $chave=>$valor to object operator -> . I also commented both if loops inside the foreach (this check does not seem relevant, since the foreach already performs them, or they are inficent if any of the properties that are going to be accessed do not exist). Also I created a small xml file (following the template you passed) to test the code execution. Then the xml.xml file looks 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 elements CONSIGNACAO (then gave to test with many elements and with only one element).

PHP code looks 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 variable 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 too:

<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>
    
04.11.2017 / 18:10