从PHP中的多维数组中删除项目 [英] Remove items from multidimensional array in PHP

查看:60
本文介绍了从PHP中的多维数组中删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要删除多维数组中的空项目。
是否有一种简单的方法可以轻松删除空项目?
我只需要保留2010-06和2010-07。

I need to remove empty items in a multidimensional array. Is there a simple way I can remove the empty items easily? I need to keep only 2010-06 and 2010-07.

非常感谢!

Array
(
    [2010-01] => Array
        (
            [2010-03] => Array
                (
                    [0] => 
                )
            [2010-04] => Array
                (
                    [0] => 
                )
            [2010-06] => Array
                (
                    [0] => stdClass Object
                        (
                            [data_test] => value
                            [date] => 2010-05-01 12:00:00
                        )

                )
            [2010-07] => Array
                (
                    [0] => stdClass Object
                        (
                            [data_test] => value
                            [date] => 2010-05-01 12:00:00
                        )

                )
        )
)


推荐答案

尝试使用此功能。

 function cleanArray($array)
{
    if (is_array($array))
    {
        foreach ($array as $key => $sub_array)
        {
            $result = cleanArray($sub_array);
            if ($result === false)
            {
                unset($array[$key]);
            }
            else
            {
                $array[$key] = $result;
            }
        }
    }

    if (empty($array))
    {
        return false;
    }

    return $array;
}

这篇关于从PHP中的多维数组中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆