Php Function that calculates the square for a series of numbers (45, 12, 1, 100, 6) the roots of each number, put them in a new array

1.8k views Asked by At

I need help for PHP function that calculates the square for a series of numbers (45, 12, 1, 100, 6) the roots of each number, put them in a new array, and prints on the screen. I know that the function is sqrt, but it receives only one parameter and here is a array of numbers. Also, Expand the previous example so that only whole values are stored in the new array numbers, and this is rounded up to the upper value.

Thank you.

2

There are 2 answers

2
Mohsen On BEST ANSWER

you can do this like blow

function ceilNumbers($array) {
    $output = array();

    foreach($array as $n) {
        array_push($output, ceil(sqrt( $n )));
    }

    return $output;
}

$arrayOfNumbers = array(69, 2, 15, 10, 85);
print_r( ceilNumbers($arrayOfNumbers) );
1
Professor Abronsius On

You could do a simple loop and use ceil to round the number up perhaps

$arr=array(45, 12, 1, 100, 6);
$out=array();

foreach( $arr as $num )$out[]=ceil( sqrt( $num ) );
print_r( $out );