0

Is there any way to return an array, something like:

$array = [
    "parcela" => "1",
    "valor"   => "100",
];

Using the function below? I need to return an array with the number of plots and the value:

Function PHP

function calcParcelaJuros($valor_total,$parcelas,$juros=0) {
    if($juros==0) {
         $string = 'PARCELA - VALOR <br />';
         for($i=1;$i<($parcelas+1);$i++) {
            $string .= $i.'x (Sem Juros) - R$.number_format($valor_total/$parcelas, 2, ",", ".").' <br />';
         }
         return $string;
     } else {
         $string = 'PARCELA - VALOR <br />';
         for($i=1;$i<($parcelas+1);$i++) {
            $I =$juros/100.00;
            $valor_parcela = $valor_total*$I*pow((1+$I),$parcelas)/(pow((1+$I),$parcelas)-1);
            $string .= $i.'x (Juros de: '.$juros.'%) - R$ '.number_format($valor_parcela, 2, ",", ".").' <br />';
         }
         return $string;
     }
}
print(calcParcelaJuros(250,4,2));

1 Answer 1

1

You only have to save the data on the array, properly formatted as you need, the use the return statement and you are good to go.

You can see, that php's return statement can return values even by reference, as it is explained here; check the second example, it will help you.


But for the sake of giving you a fast example, you can do whatever you need inside the function:

function calcParcelaJuros($valor_total,$parcelas,$juros=0) {
  $parcelas = array();

  if($juros==0) {
    for($i=1;$i<($parcelas+1);$i++) {
      ...
      ...
      $parcelas[] = ["parcela" => "1", "valor"   => "100"]
    }

    return $parcelas;
  } else {    
    ...
    ...
  }
}

You can make any process you require, save the info in the array, then return it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.