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.
This function does not look recursive. Where the recursion is?
– bfavaretto
It wouldn’t make more sense
array([0]=>200, [1]=> 50, [2] => 9)
? Which would be even simpler. Or what logic behind the algorithm?– Isac
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.
– Henrique
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.– Henrique
$f = new NumberFormatter("pt", NumberFormatter::SPELLOUT);
echo $f->format(259);
Assuming you have at least the version5.3.0
php and the extensionphp_intl
activated in thephp.ini
– Isac