Final value of the array?

Asked

Viewed 40 times

3

This variable of mine:

 $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 ???

2 answers

2

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

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

I, for safety, would take care to check if that value really is an object.

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

if ($ultima_cobranca instanceof \stdClasss)
{
   print_r($ultima_cobranca->valor);
}
  • 1

    I thought about the end() but didn’t even remember that it returned the last element, I remembered that it set the pointer to the end. + 1

  • 1

    Thank you worked, thank you

2

If you just want to take the key value valor of this array, use the function array_pop() it extracts and removes the last of the array passed. If you need to list each element use a foreach.

$ultimo = array_pop($cliente->cobrancas);
echo $ultimo->valor;
  • 1

    The only problem with array_pop is that it removes the last element of array. It may be that AP wants the array to remain intact (but he didn’t mention it in the same question)

Browser other questions tagged

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