How to remove empty values from multidimensional array in PHP?

前端 未结 8 2338
梦如初夏
梦如初夏 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: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);
    

提交回复
热议问题