Final value of the array?

3

This my life:

 $valor = $cliente->cobrancas[1]->valor;
 echo '<pre>';
 print_r($cliente);
 echo '<pre>';

Print the values below:

  stdClass Object
  (
[id] => 24151
[nome] => PAULO ANDRE ROCHA MOTTA
[endereco] => RUA CARMELA DUTRA 400 APTO 301 
[bairro] => AGRIOES
[cidade] => TERESOPOLIS
[cep] => 25963140
[uf] => RJ
[pontos] => 0
[indicados] => Array
    (
    )


[cobrancas] => Array
    (
        [0] => stdClass Object
            (
                [id] => 527561
                [nossonumero] => 3045835
                [dataemissao] => 1346900400
                [datavencimento] => 1347246000
                [datapagamento] => 1347246000
                [dias_vencidos] => 1344
                [mora] => 0.53651612903226
                [multa] => 4.158
                [valor] => 207.90
                [valorpago] => 196.47
                [valordesconto] => 10.00
                [jurosmora] => 0.00
                [banco] => 33
                [agencia] => 1525
                [dac_agencia] => 0
                [conta] => 013000045
                [dac_conta] => 2
                [carteira] => 201
                [nome_empresa] => STOCK MIDIA INF. LTDA
                [cod_cedente] => 1640348
            )

        [1] => stdClass Object
            (
                [id] => 527562
                [nossonumero] => 3045836
                [dataemissao] => 1346900400
                [datavencimento] => 1349406000
                [datapagamento] => 1349406000
                [dias_vencidos] => 1319
                [mora] => 0.30425806451613
                [multa] => 2.358
                [valor] => 117.90
                [valorpago] => 106.47
                [valordesconto] => 10.00
                [jurosmora] => 0.00
                [banco] => 33
                [agencia] => 1525
                [dac_agencia] => 0
                [conta] => 013000045
                [dac_conta] => 2
                [carteira] => 201
                [nome_empresa] => STOCK MIDIA INF. LTDA
                [cod_cedente] => 1640348
            )

        [2] => stdClass Object
            (
                [id] => 527563
                [nossonumero] => 3045837
                [dataemissao] => 1346900400
                [datavencimento] => 1352080800
                [datapagamento] => 1352080800
                [dias_vencidos] => 1288
                [mora] => 0.30425806451613
                [multa] => 2.358
                [valor] => 117.90
                [valorpago] => 106.47
                [valordesconto] => 10.00
                [jurosmora] => 0.00
                [banco] => 33
                [agencia] => 1525
                [dac_agencia] => 0
                [conta] => 013000045
                [dac_conta] => 2
                [carteira] => 201
                [nome_empresa] => STOCK MIDIA INF. LTDA
                [cod_cedente] => 1640348
            )

        [3] => stdClass Object
            (
                [id] => 527564
                [nossonumero] => 3045838
                [dataemissao] => 1346900400
                [datavencimento] => 1354672800
                [datapagamento] => 1354672800
                [dias_vencidos] => 1258
                [mora] => 0.30425806451613
                [multa] => 2.358
                [valor] => 117.90
                [valorpago] => 106.47
                [valordesconto] => 10.00
                [jurosmora] => 0.00
                [banco] => 33
                [agencia] => 1525
                [dac_agencia] => 0
                [conta] => 013000045
                [dac_conta] => 2
                [carteira] => 201
                [nome_empresa] => STOCK MIDIA INF. LTDA
                [cod_cedente] => 1640348
            )

        [4] => stdClass Object
            (
                [id] => 527565
                [nossonumero] => 3045839
                [dataemissao] => 1346900400
                [datavencimento] => 1357351200
                [datapagamento] => 1357524000
                [dias_vencidos] => 1227
                [mora] => 0.30425806451613
                [multa] => 2.358
                [valor] => 117.90
                [valorpago] => 106.47
                [valordesconto] => 10.00
                [jurosmora] => 0.00
                [banco] => 33
                [agencia] => 1525
                [dac_agencia] => 0
                [conta] => 013000045
                [dac_conta] => 2
                [carteira] => 201
                [nome_empresa] => STOCK MIDIA INF. LTDA
                [cod_cedente] => 1640348
            )

How can I make the function print always and only the last valor ???

    
asked by anonymous 16.05.2016 / 15:58

2 answers

2

If you just want to get the value of the valor key of this array, use the array_pop() function to extract it and remove the last one from the last array. If you need to list each element use a foreach.

$ultimo = array_pop($cliente->cobrancas);
echo $ultimo->valor;
    
16.05.2016 / 16:06
2

Use the end function to get the last value of array .

 $valor = end($cliente->cobrancas)->valor;

I, as a precaution, would be careful to check if that value really is an object.

$ultima_cobranca = end($cliente->cobrancas);

if ($ultima_cobranca instanceof \stdClasss)
{
   print_r($ultima_cobranca->valor);
}
    
16.05.2016 / 16:06