Home  >  Article  >  Backend Development  >  How to remove empty element index in array in php

How to remove empty element index in array in php

PHPz
PHPzOriginal
2023-04-12 09:18:57472browse

First, let us first understand what an empty element index is.

In PHP, arrays are composed of keys and values, and their corresponding relationship is like the relationship between keys and values ​​in a dictionary. In PHP, the keys in an array can be numbers or strings. When we delete an instance from the array, the instance is removed from the array, but its index remains in the array. If we delete all the elements in the array but still retain the array, it will be an empty array, and the keys of the empty array still exist. In this empty array, the value corresponding to the key is also empty, so we call it an empty element index.

If the array contains many empty element indexes, these empty elements will affect our traversal and processing of the array, so sometimes we need to remove these empty elements. Next, let's take a look at how to remove empty element indexes in the array and reset the indexes in sequence in PHP.

In PHP, there are two very useful functions that can be used to remove empty element indexes in arrays, they are array_filter() and array_values(). The first function of the array is used to filter the callback function in the array, and the second function is used to reset the index of all keys in the array.

Let us first look at the basic syntax of the array_filter() function.

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

The function accepts an array as a parameter and returns a new filtered array. The second optional parameter $callback specifies a callback function that will be called for each element in the given array. If the callback function returns true, the element will be included in the new array, if it returns false, the element will be removed from the new array.

Let's look at a practical example:

$array = array("foo", "", "bar", "", "baz");
$filtered_array = array_filter($array);
print_r($filtered_array);

In the above example, the array $array contains five elements, two of which are empty elements. Using the array_filter() function, empty elements in the array are removed and a new array $filtered_array is returned. The output is as follows:

Array
(
    [0] => foo
    [2] => bar
    [4] => baz
)

In the above result, we can see that the new array $filtered_array does not contain empty element indexes.

Next, let’s take a look at how to reset the indexes of all keys in the array. This can be achieved through the built-in array_values() function in PHP. This function accepts an array as a parameter and returns a new array in which all keys are reset to consecutive integers.

Let us look at a practical example:

$array = array("foo", "bar", "baz");
$indexed_array = array_values($array);
print_r($indexed_array);

In the above example, we give an array $array without keys. Using the array_values() function, it will be generated A new array $indexed_array in which all keys will be reset to consecutive integers. The output is as follows:

Array
(
    [0] => foo
    [1] => bar
    [2] => baz
)

In the above result, we can see that the key names in the new array $indexed_array have been correctly set to consecutive integers.

Now, we can use these two functions together to process an array containing empty element indexes and return a new array with rekeyed indexes.

Let's look at a practical example:

$array = array("foo", "", "bar", "", "baz");
$filtered_array = array_filter($array);
$indexed_array = array_values($filtered_array);
print_r($indexed_array);

In the above example, we use two functions array_filter() and array_values() and connect them together. First, we use the array_filter() function to remove empty element indexes in the array, and then use the array_values() function to reset the indexes of all keys in the new array. The output result is as follows:

Array
(
    [0] => foo
    [1] => bar
    [2] => baz
)

In the above result, we can see that the new array $indexed_array no longer contains empty element indexes, and the indexes of all keys have been reset to consecutive integers.

To sum up, by using the array_filter() function and array_values() function, we can easily handle the empty element index in the PHP array and correctly set the index of all keys in the new array. This Very helpful for our PHP code writing.

The above is the detailed content of How to remove empty element index in array 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