Get the App
SLTechnology News&Howtos  ›  Development  › 

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

Shulou Source: shulou.com Published: 2022-05-31 18:16:52 09月27日 Update

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.

Tags: Array function element field one-dimensional method variable knowledge content article result input different value parameter form simple operation article easy to understand more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Apple Xiaomi Shulou Technology MySQL