How to remove empty values from multidimensional array in PHP?

前端 未结 8 2316
梦如初夏
梦如初夏 2020-12-06 11:47

I\'ve been looking a lot of answers, but none of them are working for me.

This is the data assigned to my $quantities array:

Array(
             


        
相关标签:
8条回答
  • 2020-12-06 12:47

    use array_map() for filter every array in $array:

    $newArray = array_map('array_filter', $array);
    

    Here's demo

    0 讨论(0)
  • 2020-12-06 12:52

    The following function worked for my case. We can use a simple recursive function to remove all empty elements from the multidimensional PHP array:

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

    Then we just need to call this function:

    $myArray = array_filter_recursive($myArray);
    
    0 讨论(0)
提交回复
热议问题