In PHP, an array is a very useful data type that can store any number and type of elements. Sometimes we need to delete some elements in the array, especially null values. This article will introduce how to use PHP to delete null values in an array.
The unset() function in PHP can be used to delete elements in an array. If we want to remove null values from the array, we can do it using the unset() function.
The sample code is as follows:
<?php $myArray = array(1, 2, null, 3, null, 4); foreach ($myArray as $key => $value) { if (is_null($value)) { unset($myArray[$key]); } } print_r($myArray); ?>
Output result:
Array ( [0] => 1 [1] => 2 [3] => 3 [5] => 4 )
In this example, we first create an array containing null values. Then we use a foreach loop to traverse the array and determine whether the element is null through the is_null() function. If it is null, use the unset() function to delete the element. Finally, we use the print_r() function to print the results and find that the null value in the array has been deleted.
Note: Using the unset() function will change the original array and reset the key name of the array.
In addition to using the unset() function, we can also use the array_filter() function in PHP to delete null values in the array. The array_filter() function can filter elements in an array by specifying a callback function.
The sample code is as follows:
<?php $myArray = array(1, 2, null, 3, null, 4); $newArray = array_filter($myArray, function($element) { return !is_null($element); }); print_r($newArray); ?>
Output result:
Array ( [0] => 1 [1] => 2 [3] => 3 [5] => 4 )
In this example, we first create an array containing null values. Then, we use the array_filter() function to filter the elements in the array. In the callback function, we use the !is_null() function to determine whether the element is null. If not null, returns the element. Finally, we use the print_r() function to print the results and find that the null value in the array has been deleted.
Note: Using the array_filter() function will return a new array, and the original array will not change.
When deleting null values in an array, you can use the unset() function or the array_filter() function to complete the task. However, they differ greatly in performance.
When we use the unset() function, it will traverse the entire array and change the original array. This means that when there are a large number of elements in the array, it will become very slow.
In contrast, when we use the array_filter() function, it creates a new array and contains only elements that are not null. This means it will be faster, especially when the array is very large.
Therefore, when you need a high-performance method to remove null values in an array, it is recommended to use the array_filter() function.
Summary:
PHP is a very powerful programming language that provides a variety of methods to deal with arrays. In this article, we introduced the use of unset() function and array_filter() function to remove null values in an array. At the same time, we also compared their differences in performance. Hope this article helps you better understand how to remove null values from an array in PHP.
The above is the detailed content of php delete array null. For more information, please follow other related articles on the PHP Chinese website!