Home  >  Article  >  Backend Development  >  How to exclude empty numbers using PHP

How to exclude empty numbers using PHP

PHPz
PHPzOriginal
2023-04-04 09:01:20484browse

PHP language is an open source, object-oriented programming language, which has been widely used in website development. In data processing, we often encounter situations where there are empty numbers in the data. At this time, these empty numbers need to be eliminated to ensure the integrity and accuracy of the data. This article will explain how to exclude empty numbers using PHP.

The meaning of empty numbers means that for numeric data, if there is no specific value in the data or the value is 0, the data is called an empty number. In data processing, empty numbers are often invalid data and cannot provide effective reference for subsequent logical processing.

Let’s introduce how to use PHP to exclude empty numbers:

Method 1: Use if statement to determine

Use if statement to determine whether the number is a valid number. If so, Adding it to the array can be achieved with the following code:

//定义一个数组
$num_array=array(0,9,'',45,8,14,'',10,'',30);

//定义一个新数组
$new_array=array();

//遍历数组$num_array中的所有元素
foreach($num_array as $value){
    if(is_numeric($value)&&$value!=''){
        //将数字加入新数组中
        array_push($new_array,$value);
    }
}

//输出新数组
print_r($new_array);

Among them, the is_numeric() function is used to determine whether the variable is a number. If so, it returns true, otherwise it returns false.

In the above code, first define an array $num_array, which contains numbers and empty numbers. Next, define a new array $new_array to store valid numbers. Use a foreach loop to traverse the array $num_array, use the if statement to determine whether the number is a valid number, and if so, add it to the array $new_array. Finally, use the print_r() function to output the new array.

Run the above code, the output result is:

Array
(
    [0] => 0
    [1] => 9
    [2] => 45
    [3] => 8
    [4] => 14
    [5] => 10
    [6] => 30
)

You can see that the valid numbers have been excluded and empty numbers have been excluded.

Method 2: Use the array_filter() function

Use the array_filter() function to filter out invalid elements in the array, which can be achieved with the following code:

//定义一个数组
$num_array=array(0,9,'',45,8,14,'',10,'',30);

//过滤掉空数字
$new_array=array_filter($num_array,function($value){
    return is_numeric($value)&&($value!='');
});

//输出新数组
print_r($new_array);

In the above code , first define an array $num_array, which contains numbers and empty numbers. Then, use the array_filter() function to filter out all empty numbers and obtain a new array $new_array of valid numbers. Among them, the second parameter is the callback function, used to define filtering rules. The is_numeric() function is used in the callback function to determine whether the variable is a number. If so, it returns true, otherwise it returns false. It also determines whether the variable is an empty string. Finally, use the print_r() function to output the new array.

Run the above code, the output result is consistent with the previous method, valid numbers have been excluded and empty numbers have been excluded.

To sum up, there are many ways to exclude empty numbers using PHP. The above two methods are simple and easy to use. Which method to choose can be determined according to the actual situation. In actual development, the most appropriate method should be selected based on the characteristics and needs of the data to ensure the integrity and accuracy of the data.

The above is the detailed content of How to exclude empty numbers using PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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