Filling arrays using a recursive function?

Asked

Viewed 319 times

0

I have a recursive function that divides a certain number into parts, example: Number 259, when going through the function it will look like this: 200 and 59, I need to store these numbers in a array.

Example:

In the first loop loop loop loop that is stored the number 200 at the first position of the array and so on with all others

Function Code:

function ValorFinalArray($soma) 
{
    $tam = 0;
    do 
    {
        $tam = strlen($soma);
        if ($soma > 100 && $soma < 200) 
        {
            echo "cento <br/> e <br/>";
            echo substr($soma, -2);
            break;
        }
        $valor = $tam == 2 ? $soma : substr($soma, 0, $tam - ($tam - 1)) . str_repeat("0", $tam - ($tam - ($tam - 1)));

        if ($soma == "0")
            break;

        echo "<br/>numero: " . $valor . "<br />";

        if ($tam <= 2)
            break;

        echo "<br/> e "."<br />";

        $soma = $soma - $valor;
        $tam++;
    }
    while ($tam > 1);
}

echo ValorFinalArray(259);

the return should stay like this: array([0]=>200, [1]=> 59)

I need you to give me a hand on how I can make this process.

  • 3

    This function does not look recursive. Where the recursion is?

  • It wouldn’t make more sense array([0]=>200, [1]=> 50, [2] => 9) ? Which would be even simpler. Or what logic behind the algorithm?

  • then, this algorithm I use it to verbalize the numbers in a central Telephonic, in this case the value of the client’s overdue accounts. i receive from a web service the value, do the treatment and pass to this function, with the return of it I can make the central "tell" to the client the value in real of his account.

  • I agree with you that it would be more logical for the return to be this way array([0]=>200, [1]=> 50, [2] => 9), but I had tried with other numbers and it had not worked.

  • $f = new NumberFormatter("pt", NumberFormatter::SPELLOUT);&#xA;echo $f->format(259); Assuming you have at least the version 5.3.0 php and the extension php_intl activated in the php.ini

1 answer

0

Just take the generated values and insert in a array in the case a variable was created $ret of the kind array and the generated values added to that array and at the end of that function, gives a return $ret, example:

function ValorFinalArray($soma) 
{
    $ret = array();
    $tam = 0;
    do 
    {
        $tam = strlen($soma);
        if ($soma > 100 && $soma < 200) 
        {
            echo "cento <br/> e <br/>";
            echo substr($soma, -2);
            break;
        }
        $valor = $tam == 2 ? 
           $soma : 
           substr($soma, 0, $tam - ($tam - 1)) . 
           str_repeat("0", $tam - ($tam - ($tam - 1)));

        if ($soma == "0")
            break;

        $ret[] = (int)$valor;

        if ($tam <= 2)
            break;

        $soma = $soma - $valor;
        $tam++;
    }
    while ($tam > 1);
    return $ret;
}

var_dump ( ValorFinalArray(259) );

I believe that your code only serves for this specific number, but, I did not take into account what this function would generate, but, I demonstrated how this return can be made in a array.

Online Example

  • Thank you Virgilio, that’s exactly what I needed.

Browser other questions tagged

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