PHP Array Exercises : Return the lowest integer (not 0) from a list of numbers
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:
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.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics