Quickly return the smallest integer that is not 0 using PHP

藏色散人
Release: 2023-04-10 13:42:02
Original
2275 people have browsed it

In the previous article "Using the Pearl Algorithm to Sort PHP Arrays", I introduced you to sorting arrays through the Pearl Algorithm. This article continues to introduce you to a little knowledge point~

Suppose there is such a problem: Please write a PHP function that returns the smallest integer that is not 0.

Do you have any simple ideas for this problem?

In fact, everyone should know that there is a built-in function min() in PHP, which is used to return the minimum value in an array, or the minimum value among several specified values.

So how do we create a PHP function to get the smallest integer that returns not 0?

Let’s introduce the specific implementation steps to you:

First create a PHP sample file demo.php;

Then customize a min_values_not_zeroh Function;

The complete code is as follows:

<?php
function min_values_not_zero(Array $values)
{
    return min(array_diff(array_map(&#39;intval&#39;, $values), array(0)));
}
print_r(min_values_not_zero(array(-1,0,1,12,-100,1))."\n");
Copy after login

Here is a sample array "-1,0,1,12,-100,1", let us take a look at this set What is the smallest integer that the data returns is not 0?

The results are as follows:

Quickly return the smallest integer that is not 0 using PHP

As shown in the figure, the smallest integer is -100.

It’s very simple.

Note:

array_map()Function: Apply the callback function to each element of the array. The return value is an array, including the array after the callback function is processed (there are multiple For arrays, arrays) corresponds to indexing all elements as parameters of the function. When only an array is passed in, the returned array retains the keys of the passed parameters. When passing in multiple arrays, the returned array keys are sequential integers.

array_diff()Function: Used to compare the values ​​​​of two (or more) arrays and return the difference set. This function compares the values ​​of two (or more) arrays (value in key=>value) and returns a difference array that includes all the values ​​in the compared array (array1) but not in any Values ​​in other parameter arrays (array2 or array3, etc.).

Finally, I would like to recommend to you the latest free course on our platform "Entering the World of PHP from 0"~ Come and learn!

The above is the detailed content of Quickly return the smallest integer that is not 0 using PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!