Home  >  Article  >  Backend Development  >  How to remove empty arrays in multidimensional arrays in php

How to remove empty arrays in multidimensional arrays in php

PHPz
PHPzOriginal
2023-04-19 11:39:32583browse

With the development of PHP, PHP has become one of the most widely used languages ​​in the field of web development. In actual development, developers often need to deal with empty arrays in multi-dimensional arrays, which not only wastes memory, but also has a great impact on data processing and code efficiency. Therefore, this article will discuss how to quickly remove empty arrays from multi-dimensional arrays in PHP and improve the efficiency of the code.

1. Definition of multidimensional arrays and empty arrays

In PHP, a multidimensional array means that the array contains another array. For example:

$multi_array = array(
    array('A','B','C'),
    array('D','E','F'),
    array('G','H','I')
);

An empty array refers to an array that is empty (that is, has no value) at a certain position in a multidimensional array. For example:

$multi_array = array(
    array('A','B',),
    array('','F','G'),
    array('','J','K')
);

In the above example, the first position of the second row and the first position of the third row are empty arrays.

2. Methods to remove empty arrays

1. Use foreach loop

This is the most basic method. We can use foreach to loop a multi-dimensional array and use it according to the situation. The unset() function removes empty arrays. This method is simple and easy to understand, but it will have a certain impact on code efficiency, especially when processing large-scale data.

foreach($multi_array as $k=>$v){
    if(is_array($v)){
        foreach($v as $key=>$val){
            if($val === '' || $val === null){ 
                unset($multi_array[$k][$key]);
             }
        }
    }
    if(empty($multi_array[$k])){ 
        unset($multi_array[$k]); 
    }
}

2. Use the array_filter() function

The array_filter() function can filter each element in a multi-dimensional array and return elements whose array value is not empty. This function can only remove null values ​​​​in one-dimensional arrays, therefore, we need to use the array_map() function to process multi-dimensional arrays.

function array_filter_recursive($input)
{
    foreach($input as &$value) {
        if(is_array($value)){
            $value = array_filter_recursive($value);
        }
    }
    return array_filter($input);
}

$multi_array = array_filter_recursive($multi_array);

3. Use the array_walk() function

The array_walk() function is used to traverse each element of the array, allowing developers to process data in the callback function. In this method, we calculate the number of elements in the multidimensional array through the callback function. If the number of elements is 0, the array is deleted from the multidimensional array.

function remove_empty_arrays(&$value)
{
    if (is_array($value)) {
        $value = array_filter($value, 'remove_empty_arrays');
    }
    return (!empty($value) || $value === '0');
}

array_walk($multi_array, 'remove_empty_arrays');

3. Summary

In PHP, dealing with empty arrays in multi-dimensional arrays is often one of the problems that developers need to solve. This article introduces three common processing methods: using a foreach loop, using the array_filter() function, and using the array_walk() function. For large-scale data processing, we should choose more efficient methods to improve the efficiency and performance of the code.

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