PHP array is a very commonly used data structure. It consists of many data items. During the development process, we often need to remove some unnecessary data from the array, such as empty data. This article will introduce how to use PHP code to remove empty data from an array.
What is empty data?
In PHP, empty data usually refers to string type data, used to indicate the absence of a value. It may be composed of spaces, tabs, newlines and other characters. When we use arrays, sometimes there will be some unnecessary empty data. These empty data will affect our operations on the array, so we need to remove them from the array.
Use the unset() function to remove empty data
In PHP, we can use the unset() function to delete one or more elements from an array. This function takes one or more keys as parameters, and it will delete the elements corresponding to these keys from the array.
The following is an example array:
$data = array( 'name' => 'John', 'age' => '', 'email' => 'john@example.com', 'gender' => null, 'city' => false );
The array contains an empty string, a null value and a false value. We can use the unset() function to delete these elements, as follows Indication:
foreach ($data as $key => $value) { if ($value === '' || $value === null || $value === false) { unset($data[$key]); } }
In the above code, we use a foreach loop to traverse all the elements in the array. If the value of the element is any one of the empty string, null value or false value, we will use unset( ) function to delete it. This way we can get an array without empty data.
Use the array_filter() function to remove empty data
In addition to using the unset() function, we can also use the array_filter() function to filter out empty data in the array. This function can be used to filter out any unwanted data.
The following is a sample code that uses the array_filter() function to remove empty data:
$data = array( 'name' => 'John', 'age' => '', 'email' => 'john@example.com', 'gender' => null, 'city' => false ); $data = array_filter($data, function ($value) { return $value !== '' && $value !== null && $value !== false; }); print_r($data);
This sample code using the array_filter() function is very simple. We only need to filter the array and a Just pass the anonymous function to it as a parameter. The function of the anonymous function is to filter each element in the array. The return value of the anonymous function is used to determine whether to filter the element. If true is returned, the element is retained, otherwise the element is filtered out. In this example, we return true to retain all non-null data.
Conclusion
PHP array is a very commonly used data structure, but sometimes we need to remove some of the empty data in order to better operate the array. This article introduces two methods to remove empty data in an array, namely using the unset() function and using the array_filter() function. Both methods are simple and easy to understand. You can choose the appropriate method according to your own needs when using it. .
The above is the detailed content of Remove empty data from php array. For more information, please follow other related articles on the PHP Chinese website!