Home  >  Article  >  Backend Development  >  How to delete a field in an array that is empty in php

How to delete a field in an array that is empty in php

PHPz
PHPzOriginal
2023-04-20 15:04:59548browse

In the process of using PHP arrays, some fields or values ​​​​often appear to be empty. If we need to process these data, we need to delete the elements with empty fields. This article will introduce how to use PHP to delete an empty field in an array.

  1. Use the unset() function to delete elements with empty fields

First, we can use the unset() function to delete an empty field in the array. This function removes the specified element from the array and returns the value of the removed element while reducing the size of the original array. The following is an example code that uses the unset() function to delete an empty field in an array:

$array = array(
    array('id'=>1, 'name'=>'Tom', 'age'=>''),
    array('id'=>2, 'name'=>'Jack', 'age'=>18),
    array('id'=>3, 'name'=>'Kate', 'age'=>''),
);

foreach($array as $key => $value){
    if($value['age'] == ''){
        unset($array[$key]);
    }
}

print_r($array); //输出结果:Array ( [1] => Array ( [id] => 2 [name] => Jack [age] => 18 ) )

In the above code, we first define a two-dimensional array containing three elements, in which each Each element contains three fields: id, name and age. Then, we use a foreach loop to traverse the array, and when we find that the age field in an element is empty, we use the unset() function to delete the element.

Note: When using the unset() function to delete array elements, the index of the array will be rearranged, so when traversing the array, we need to use a foreach loop to operate to avoid index errors during the deletion process. question.

  1. Use the array_filter() function to delete elements with empty fields

In addition to using the unset() function, we can also use the array_filter() function to delete an element in the array. Elements with empty fields. This function can filter the elements in the array and return the filtered array, while the original array will not change in size. The following is an example code that uses the array_filter() function to delete an empty field in an array:

$array = array(
    array('id'=>1, 'name'=>'Tom', 'age'=>''),
    array('id'=>2, 'name'=>'Jack', 'age'=>18),
    array('id'=>3, 'name'=>'Kate', 'age'=>''),
);

function filter($value){
    return $value['age'] != '';
}

$array = array_filter($array, 'filter');

print_r($array); //输出结果:Array ( [1] => Array ( [id] => 2 [name] => Jack [age] => 18 ) )

In the above code, we first define a two-dimensional array array containing three elements, and then define A function named filter, which is used to filter elements in the array whose age field is empty. Finally, we use the array_filter() function to filter the array, and use the filter() function as a callback function to process and return the new filtered array.

Comparison of the two methods:

Using both the unset() function and the array_filter() function can complete the operation of deleting elements in an array where a field is empty. However, there are still some differences between them. In terms of code complexity, using the unset() function is more concise and clearer; using the array_filter() function requires defining a callback function, which requires a larger amount of code. However, in terms of code function performance, the array_filter() function is more flexible and can handle filtering operations on multiple different fields at the same time.

Summary:

This article introduces how to use PHP to delete a field in an array that is empty, including using the unset() function and the array_filter() function. Readers can choose different methods to operate according to their needs to achieve better results. In addition, during use, optimization can be carried out according to the actual situation to improve the performance and efficiency of the code.

The above is the detailed content of How to delete a field in an array that is empty in 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