w3resource

PHP Array Exercises : Return the lowest integer (not 0) from a list of numbers

PHP Array: Exercise-17 with Solution

Write a PHP function that returns the lowest integer that is not 0.

Sample Solution:

PHP Code:

<?php
// Define a function named 'min_values_not_zero' that takes an array of values as input
function min_values_not_zero(Array $values)
{
    // Use 'array_map' to convert each element to an integer, 'array_diff' to exclude zero, and 'min' to find the minimum value
    return min(array_diff(array_map('intval', $values), array(0)));
}

// Call the 'min_values_not_zero' function with an example array and print the result
print_r(min_values_not_zero(array(-1, 0, 1, 12, -100, 1)) . "\n");
?>

Output:

-100   

Flowchart:

Flowchart: Return the lowest integer (not 0) from a list of numbers

PHP Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a PHP script to get the largest key in an array.
Next: Write a PHP function to floor decimal numbers with precision.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/php-exercises/php-array-exercise-17.php