Current Location: Home> Latest Articles> Example of combination application of is_nan and array_map in PHP

Example of combination application of is_nan and array_map in PHP

gitbox 2025-05-27

In PHP, the is_nan function is a very practical tool to check if a value is "non-number" (NaN). It returns a Boolean value, which returns true if the value passed in is NaN (i.e. not a valid number), otherwise returns false .

The array_map function allows us to apply a callback function to each element in the array, returning the processed new array. When these two functions are used in combination, we can easily handle arrays efficiently, especially when filtering or converting the array with "non-numeric" values.

1. The role of is_nan function

Before you dive into how to use is_nan with array_map , it is very important to first understand the basic functions of is_nan . The syntax of the is_nan function is as follows:

 is_nan(mixed $value): bool
  • $value : The value to be checked.

  • Return value: true if $value is NaN , otherwise false .

Example:

 var_dump(is_nan(123)); // false
var_dump(is_nan(NAN)); // true
var_dump(is_nan("Hello")); // false

2. The role of array_map function

The array_map function is used to apply a callback function to each element in the array. Its basic syntax is as follows:

 array_map(callable $callback, array $array, array ...$arrays): array
  • $callback : A callback function applied to each element in the array.

  • $array : The array to be processed.

  • Return value: A new array containing the value processed by the callback function.

Example:

 $numbers = [1, 2, 3, 4];
$squares = array_map(function($n) {
    return $n * $n;
}, $numbers);

print_r($squares); // Output [1, 4, 9, 16]

3. Use is_nan and array_map in combination

Using is_nan and array_map can effectively filter or convert NaN values ​​in an array. For example, if you have an array containing some valid numbers and NaN values, you might want to convert all NaN values ​​to zero or other default values.

Example: Replace NaN with zero

Suppose we have an array with some numbers and NaN values, and we want to replace all NaN values ​​with zeros.

 $array = [1, 2, NAN, 4, NAN, 6];

$result = array_map(function($value) {
    return is_nan($value) ? 0 : $value;
}, $array);

print_r($result); 
// Output [1, 2, 0, 4, 0, 6]

Example: Remove all NaN values

If you want to remove all NaN values ​​from the array, you can combine the array_filter and is_nan functions. array_filter will filter out elements that return false , and is_nan checks whether the value is NaN .

 $array = [1, 2, NAN, 4, NAN, 6];

$result = array_filter($array, function($value) {
    return !is_nan($value);
});

print_r($result); 
// Output [1, 2, 4, 6]

Example: Convert the NaN value in the array to a specific string

Suppose you want to replace all NaN values ​​in the array with a string, such as "Invalid" . You can do this:

 $array = [1, 2, NAN, 4, NAN, 6];

$result = array_map(function($value) {
    return is_nan($value) ? "Invalid" : $value;
}, $array);

print_r($result); 
// Output [1, 2, "Invalid", 4, "Invalid", 6]

4. Practical application scenarios

Data cleaning and conversion

In practical applications, it is often necessary to clean up the data entered by the user. For example, data entered by a user may contain illegal numbers (such as NaN ). With is_nan and array_map , you can quickly clean up this data to ensure that the application processes valid numbers.

Mathematical calculations and statistics

NaN values ​​may occur in some cases when performing mathematical calculations or statistical analysis. Using is_nan to detect and process these values ​​can avoid calculation errors or inconsistent results.

5. Summary

By combining is_nan and array_map functions, PHP developers can efficiently process NaN values ​​in arrays, whether filtering, substituting, or converting. This method is not only concise but also has good performance, and is suitable for various data processing scenarios.

In practical applications, rational use of these two functions can help us better manage and process complex data sets and ensure the stability and reliability of the program.

Hope this article helps you! If you have any questions or want to know more about PHP programming skills, please feel free to communicate.