How does php delete an empty field in an array?

04-03-2024

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

Use the unset () function to delete an element with an empty field. First, we can use the unset () function to delete an empty field in the array. This function can remove the elements specified in the array and return the values of the removed elements, while the size of the original array will be reduced. 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); //Output result: array ([1] = > array ([ID] = > 2 [name] = > jack [age] = > 18))

In the above code, we first define a two-dimensional array array with three elements, each of which contains three fields: id, name and age. Then, we use the 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 unset () function to delete array elements, the indexes of the array will be rearranged, so when traversing the array, we need to use foreach loop to avoid the problem of index error during deletion.

Using the array_filter () function to delete an element with an empty field In addition to the unset () function, we can also use the array_filter () function to delete an element with an empty field in the array. This function can filter the elements in the array and return the filtered array without changing the size of the original array. 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); //Output result: array ([1] = > array ([ID] = > 2 [name] = > jack [age] = > 18))

In the above code, we first define a two-dimensional array with three elements, and then define a function called filter, which is used to filter the 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 filtered new array.

Comparison of two methods:

Both unset () function and array_filter () function can be used to delete an element with an empty field in the array. However, there are some differences between them. Compared with the code complexity, using unset () function is more concise and clear. Using the array_filter () function needs to define a callback function, which will have more code. However, the array_filter () function is more flexible in terms of code function performance, and it can handle filtering operations of multiple different fields at the same time.

Summary:

This paper introduces how to use PHP to delete an empty field in an array, including using unset () function and array_filter () function. Readers can choose different methods according to their needs to achieve better results. In addition, in the process of use, it can also be optimized according to the actual situation to improve the performance and efficiency of the code.

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us