How do I return all splits in this function in [PHP]?

1

Good evening everyone. I'm stuck in a stalemate I can not solve. I need to return 11 times $tot_formatado which corresponds to exactly one time for each installment rate present inside the array.

If you repair the $tot_formatado , I put $array[0] which returns me the price for the $array[0] that corresponds to $parc_two which is 0.055 .

You need to return the 11 values for each index of the array used for the calculation. Example in the background.

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 to looping and arrays but I'm not sure how to do it.

    
asked by anonymous 29.04.2015 / 23:37

2 answers

2

Associate the values in the array, one in each index, then perform the calculation for each plot 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;
}
    
30.04.2015 / 00:17
2

You can not return 11 values at once with the 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.

So:

//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
    
30.04.2015 / 00:21