Current Location: Home> Latest Articles> How to rebuild an index array with array_filter() + array_values()

How to rebuild an index array with array_filter() + array_values()

M66 2025-06-04

In PHP, arrays are very commonly used data structures. During the development process, we often encounter situations where we need to remove the empty value of the array and rebuild the array index. Fortunately, PHP provides array_filter() and array_values() functions to help us easily accomplish this task.

1. Introduction to array_filter() function

The array_filter() function is used to filter elements in an array. By default, array_filter() removes elements with a value of false (such as null , false , 0 , "" etc.). This function returns a new array containing all elements in the original array that are not false .

Sample code:

 $array = [1, 0, 2, null, 3, '', 4];
$filteredArray = array_filter($array);
print_r($filteredArray);

Output result:

 Array
(
    [0] => 1
    [2] => 2
    [4] => 3
    [6] => 4
)

As shown above, the array_filter() function removes the null value in the array, but it does not rebuild the index of the array, resulting in the returned array still retaining the index of the original array.

2. Introduction to array_values() function

The array_values() function returns a new array of all values ​​in the array and re-index it. Usually, after filtering the array, we use array_values() to reindex the array.

Sample code:

 $array = [1, 0, 2, null, 3, '', 4];
$filteredArray = array_filter($array);
$reindexedArray = array_values($filteredArray);
print_r($reindexedArray);

Output result:

 Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)

By using array_values() we successfully rebuild the index of the array and remove all null values.

3. Comprehensive example: Filter empty values ​​and rebuild array index

We can combine array_filter() and array_values() to create a practical function to filter null values ​​in an array and re-index:

 function cleanAndReindexArray($array) {
    $filtered = array_filter($array); // Filter empty values
    return array_values($filtered);   // Reindex array
}

$array = [1, 0, 2, null, 3, '', 4];
$cleanedArray = cleanAndReindexArray($array);
print_r($cleanedArray);

Output result:

 Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)

4. Handle domain name replacement in URL

If your array contains URLs and you need to replace the domain name with m66.net , we can do it with array_map() and regular expressions.

Sample code:

 $array = [
    'https://example.com/page1',
    'http://test.com/page2',
    'https://anotherdomain.com/page3'
];

$replacedArray = array_map(function($url) {
    return preg_replace('/https?:\/\/([^\/]+)/', 'https://m66.net', $url);
}, $array);

print_r($replacedArray);

Output result:

 Array
(
    [0] => https://m66.net/page1
    [1] => http://m66.net/page2
    [2] => https://m66.net/page3
)

In the example above, we used preg_replace() to replace the domain name in the URL and changed it to m66.net .

Summarize