Return portions of an array by full (string)-PHP

Asked

Viewed 47 times

0

Hello,

I have a array of plots, and I want to return by extenso, briefly to quantidade de parcelas and the valores.

Example

  • Entrance of R $ 15,00 + 3 installments of R $ 15,00

In my test code, I could not get this result, even using foreach

<?php

    $installments = [];
    for ($x = 0; $x < 4; $x++) {
        $installments[$x]['installment'] = $x + 1;
        $installments[$x]['value'] = 15.0;
    }
    $policy['installment'] = $installments;
    $policy['in_full']     = get_in_full($installments);

    function get_in_full($installments) {
        foreach ($installments as $installment)
        {
            return 'result';
        }
        //return 'Entrada de R$ 15,00 + 3 parcelas de R$ 15,48';
    }

    echo '<pre>';
    print_r($policy);
    echo '</pre>';

?>

inserir a descrição da imagem aqui

1 answer

2


With the following code I got the result you want.

function get_in_full($installments) {
  // Valor da entrada
  $value = number_format($installments[0]['value'], 2);

  // Valor da primeira parcela
  $installmentValue = number_format($installments[1]['value'], 2);

  return 'Entrada de R$ '. $value .' + '. (count($installments) - 1) .' parcelas de R$ '. $installmentValue;
}

Browser other questions tagged

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