How to divide a variable by the values of an array in php

Asked

Viewed 98 times

-1


I want to take the value of a variable and make a division by the first value in the array and the result divide by the next value until it reaches 1. I was able to do the division, but it is not the expected result.
The value for splitting is 60 and the values for splitting is in the array that is [5,4,3,2,1], thus it gives the following result [60,30,10,5,1]. The result I need would be that [12,3,1] and when the value of the division reaches 1 it closes the division.

Understanding the result
60/5 = 12
12/4 = 3
3/3 = 1

The code I tried to make

function divisaoNoArray($x, $y)
{
        for($i=0; $i < count($x); $i++):
            $a[] = $i+1;
            rsort($a);
            array_splice($a, -1, 1, $y /= $a[$i]);
        endfor;
    return json_encode($a);
}

$array = array('A', 'B', 'C', 'D', 'E');
$num = 60;
echo divisaoNoArray($array, $num);


Resultado
[60,30,10,5,1]

Can someone help me resolve this issue?
I thank you all in advance

  • In the result only integers?

  • Hello colleague, yes it has to be whole numbers. I want to embed in a script to bring a result using a math method that is the Simple Arrangement and put all its combinations.

1 answer

0


Type Thus?

function divisao($n, $a){
    for ($i=1; $n > 1; $i++) {
        $n = $n / $a[$i-1];
        echo "Divisão ".$i.": ".$n."<br>";
    }
}

divisao(60, [5, 4, 3, 2, 1])

Browser other questions tagged

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