How to return all installments in this function in [PHP]?

Asked

Viewed 325 times

1

Good evening, everyone. I’m stuck in an impasse I can’t resolve. I need to return 11 times $tot_formatado which corresponds to exactly once for each parceling rate present within the array.

If you look at the $tot_formatado, put there $array[0] that returns me the price for the $array[0] which corresponds to the $parc_two that is of 0.055.

You need to return the 11 values each for each input of the array used for the calculation. Example at the bottom.

function parcelamento ($valor){

        $taxa_intermediacao = 0.064; // porcentagem
        $taxa_processamento = 0.60; // monetário
        $array = array (
            $parc_two => 0.055, // porcentagem
            $parc_tree => 0.060, 
            $parc_four => 0.065, 
            $parc_five => 0.075, 
            $parc_six => 0.085, 
            $parc_seven => 0.095,
            $parc_eight => 0.105,
            $parc_nine => 0.115,
            $parc_ten => 0.125,
            $parc_eleven => 0.130,
            $parc_twelve => 0.135
        );

        $tot_formatado = number_format ( ( $valor - ( $valor * ( $array[0] + $taxa_intermediacao ) ) - 0.60 ), 2 );
        return $tot_formatado;

    }

    parcelamento($valor);

I think this has some relationship with loop and arrays but I don’t know exactly how to do.

2 answers

2

Associate the values in the array, one in each Dice, then perform the calculation for each installment and insert into a new array:

function parcelamento ($valor){

$taxa_intermediacao = 0.064; // porcentagem
$taxa_processamento = 0.60; // monetário
$array = array (
    'parc_two' => 0.055, // porcentagem
    'parc_tree' => 0.060, 
    'parc_four' => 0.065, 
    'parc_five' => 0.075, 
    'parc_six' => 0.085, 
    'parc_seven' => 0.095,
    'parc_eight' => 0.105,
    'parc_nine' => 0.115,
    'parc_ten' => 0.125,
    'parc_eleven' => 0.130,
    'parc_twelve' => 0.135
);


 $tot_formatado = array();        
  foreach($array as $taxa){
        $tot_formatado[] = number_format ( ( $valor - ( $valor * ( $taxa + $taxa_intermediacao ) ) - 0.60 ), 2 );
    }

    return $tot_formatado;

}

$parcelas = parcelamento($valor);
foreach($parcelas as $parcela){
    echo 'Valor '.$parcela;
}

2

You cannot return 11 values at once with Return, an alternative is to calculate all the plots in a separate array and return that array, another is to put another attribute in the function that will return the desired value.

Thus:

//via array
<?php
function parcelamento($valor) {

    $taxa_intermediacao = 0.064; // porcentagem
    $taxa_processamento = 0.60; // monetário
    $array = array (
        $parc_two => 0.055, // porcentagem
        $parc_tree => 0.060, 
        $parc_four => 0.065, 
        $parc_five => 0.075, 
        $parc_six => 0.085, 
        $parc_seven => 0.095,
        $parc_eight => 0.105,
        $parc_nine => 0.115,
        $parc_ten => 0.125,
        $parc_eleven => 0.130,
        $parc_twelve => 0.135
    );

    $tot_formatado = array();
    foreach($array as $parc) {

        $tot_formatado = number_format ( ( $valor - ( $valor * ( $parc + $taxa_intermediacao ) ) - 0.60 ), 2 );
    }

    return $tot_formatado;

}

$results = parcelamento($valor);
foreach($results as $value) {
    echo $value;
}

Or so:

//via parametro
<?php
function parcelamento($valor, $parcela) {

    $taxa_intermediacao = 0.064; // porcentagem
    $taxa_processamento = 0.60; // monetário
    $array = array (
        $parc_two => 0.055, // porcentagem
        $parc_tree => 0.060, 
        $parc_four => 0.065, 
        $parc_five => 0.075, 
        $parc_six => 0.085, 
        $parc_seven => 0.095,
        $parc_eight => 0.105,
        $parc_nine => 0.115,
        $parc_ten => 0.125,
        $parc_eleven => 0.130,
        $parc_twelve => 0.135
    );

    $tot_formatado = number_format ( ( $valor - ( $valor * ( $array[$parcela] + $taxa_intermediacao ) ) - 0.60 ), 2 );
    return $tot_formatado;

}

echo parcelamento($valor, 0); // ou outra parcela $parc_two, etc...
//pode fazer um for para chamar outras parcelas

Browser other questions tagged

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