PHP - How to remove empty entries of an array recursively?

前端 未结 6 1143
臣服心动
臣服心动 2020-12-15 22:58

I need to remove empty entries on multilevel arrays. For now I can remove entries with empty sub-arrays, but not empty arrays... confused, so do I... I think the code will h

6条回答
  •  失恋的感觉
    2020-12-15 23:37

    My function:

    function removeEmptyItems($item)
    {
        if (is_array($item)) {
            $item = array_filter($item, 'removeEmptyItems');
        }
        return !empty($item);
    }
    
    $nonEmpty = array_filter($raw, 'removeEmptyItems');
    

提交回复
热议问题