Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to remove empty fields from one-dimensional array by php

2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces php how to remove the empty field of the one-dimensional array of related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that after reading this php how to remove the empty field of the one-dimensional array article will have a harvest, let's take a look.

Methods: 1, using the "array_filter ()" function, the function can use the callback function to filter the elements in the array, and the syntax is "array_filter (array, callback function)"; 2, use foreach to traverse the array elements, cooperate with the empty () function to find out the empty field, and delete it with the unset () function.

This article operating environment: Windows10 system, PHP7.1 version, Dell G3 computer

How to remove the empty field of one-dimensional array by php

Method 1: use array_filter function

The array_filter () function uses the callback function to filter elements in the array.

This function passes each key value in the input array to the callback function. If the callback function returns true, the current key value in the input array is returned to the result array. The array key name remains the same.

Array array_filter (array $array [, callable $callback [, int $flag = 0]])

Array is required. Specifies the array to filter.

Callback is optional. Specifies the callback function to be used.

Flag is optional. Determine the form of parameters received by callback

Method 2: use foreach to traverse array elements and find empty elements to delete.

The empty () function is used to check whether a variable is empty.

Unset ()-releases the given variable.

Void unset (mixed $var [, mixed $...])

$var: the variable to destroy.

The behavior of unset () in the function varies depending on the type of variable you want to destroy.

Examples are as follows:

Output result:

This is the end of the article on "how php removes the empty fields of one-dimensional arrays". Thank you for reading! I believe you all have a certain understanding of "how php removes the empty fields of one-dimensional array". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report