PHP嵌套数组按条件删除特定键

时间:2014-08-29 07:00:58

标签: php

我正在使用PHP 5.5.2后端MySQL。我有一个self-referencing database table,我得到array,如下所示:

Array
(
    [0] => Array
        (
            [id] => 47
            [s_id] => 
            [m_id] => 
            [title] => Aaa
            [type] => G
            [created_at] => 2014-08-29 06:05:18
            [frequency] => d
            [start] => 2014-08-29
            [time] => 09:00
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 48
                            [s_id] => 37
                            [m_id] => 
                            [title] => bbbbbbbbbbbbb
                            [type] => Q
                            [created_at] => 2014-08-29 06:05:18
                            [frequency] => 
                            [start] => 
                            [time] => 
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 49
                                            [s_id] => 38
                                            [m_id] => 
                                            [title] => cccccccccc
                                            [type] => A
                                            [created_at] => 2014-08-29 06:05:18
                                            [frequency] => 
                                            [start] => 
                                            [time] => 
                                            [children] => Array
                                                (
                                                )
                                        )

                                    [1] => Array
                                        (
                                            [id] => 50
                                            [s_id] => 39
                                            [m_id] => 
                                            [title] => ddddddddd dddd 
                                            [type] => A
                                            [created_at] => 2014-08-29 06:05:18
                                            [frequency] => 
                                            [start] => 
                                            [time] => 
                                            [children] => Array
                                                (
                                                )
                                        )

                                    [2] => Array
                                        (
                                            [id] => 51
                                            [s_id] => 40
                                            [m_id] => 
                                            [title] => eeeeeee eeeeeee.
                                            [type] => A
                                            [created_at] => 2014-08-29 06:05:18
                                            [frequency] => 
                                            [start] => 
                                            [children] => Array
                                                (
                                                )
                                        )
                                )
                        )

                    [1] => Array
                        (
                            [id] => 54
                            [s_id] => 18
                            [m_id] => 
                            [title] => fffffffff ffff
                            [type] => Q
                            [created_at] => 2014-08-29 06:05:18
                            [frequency] => 
                            [start] => 
                            [time] => 
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 55
                                            [s_id] => 19
                                            [m_id] => 
                                            [title] => gggggg gggggg
                                            [type] => A
                                            [created_at] => 2014-08-29 06:05:18
                                            [frequency] => 
                                            [start] => 
                                            [time] => 
                                            [children] => Array
                                                (
                                                )
                                        )

                                    [1] => Array
                                        (
                                            [id] => 56
                                            [s_id] => 20
                                            [m_id] => 
                                            [title] => hhhhhhhhhh hhhh
                                            [type] => A
                                            [created_at] => 2014-08-29 06:05:18
                                            [frequency] => 
                                            [start] => 
                                            [children] => Array
                                                (
                                                )
                                        )
                                )
                        )

                    [2] => Array
                        (
                            [id] => 57
                            [s_id] => 21
                            [m_id] => 
                            [title] => iiiiiiii iiii
                            [type] => Q
                            [created_at] => 2014-08-29 06:05:18
                            [frequency] => 
                            [start] => 
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 58
                                            [s_id] => 22
                                            [m_id] => 
                                            [title] => jjjjjjj jjj
                                            [type] => A
                                            [created_at] => 2014-08-29 06:05:18
                                            [frequency] => 
                                            [start] => 
                                            [time] => 
                                            [children] => Array
                                                (
                                                )
                                        )

                                    [1] => Array
                                        (
                                            [id] => 59
                                            [s_id] => 23
                                            [m_id] => 
                                            [title] => kkkkkkkk kkkk
                                            [type] => A
                                            [created_at] => 2014-08-29 06:05:18
                                            [frequency] => 
                                            [start] => 
                                            [time] => 
                                            [children] => Array
                                                (
                                                )
                                        )
                                )
                        )
                )
        )
        [1] => Array
        (
            [id] => 47
            [s_id] => 
            [m_id] => 
            [title] => xxxx xxx xxx 
            [type] => G
            [created_at] => 2014-08-29 06:05:18
            [frequency] => d
            [start] => 2014-08-29
            [time] => 09:00
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 112
                            [s_id] => 37
                            [m_id] => 
                            [title] => lllllllllll llllll
                            [type] => Q
                            [created_at] => 2014-08-29 06:05:18
                            [frequency] => 
                            [start] => 
                            [time] => 
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 113
                                            [s_id] => 38
                                            [m_id] => 
                                            [title] => mmmmmmm mmmm
                                            [type] => A
                                            [created_at] => 2014-08-29 06:05:18
                                            [frequency] => 
                                            [start] => 
                                            [time] => 
                                            [children] => Array
                                                (
                                                )
                                        )

                                    [1] => Array
                                        (
                                            [id] => 123
                                            [s_id] => 39
                                            [m_id] => 
                                            [title] => nnnnnnnnn hhhhh
                                            [type] => A
                                            [created_at] => 2014-08-29 06:05:18
                                            [frequency] => 
                                            [start] => 
                                            [time] => 
                                            [children] => Array
                                                (
                                                )
                                        )
                                )
                        )

                    [1] => Array
                        (
                            [id] => 456
                            [s_id] => 18
                            [m_id] => 
                            [title] => ppppppp pppppp
                            [type] => Q
                            [created_at] => 2014-08-29 06:05:18
                            [frequency] => 
                            [start] => 
                            [time] => 
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 545
                                            [s_id] => 19
                                            [m_id] => 
                                            [title] => qqqqqqqqq qqq
                                            [type] => A
                                            [created_at] => 2014-08-29 06:05:18
                                            [frequency] => 
                                            [start] => 
                                            [time] => 
                                            [children] => Array
                                                (
                                                )
                                        )

                                    [1] => Array
                                        (
                                            [id] => 435
                                            [s_id] => 20
                                            [m_id] => 
                                            [title] => ssssssssss sssssssssss 
                                            [type] => A
                                            [created_at] => 2014-08-29 06:05:18
                                            [frequency] => 
                                            [start] => 
                                            [children] => Array
                                                (
                                                )
                                        )
                                )
                        )
                )
        )
)

N nesting levels中可能有array

现在对于每个数组元素,如果['type']G,那么我想要remove keys [s_id][m_id]以及any other type我希望remove keys [start], [end] }和[time]

编辑:我尝试使用递归函数,但没有给出所需的结果。

private function getarray(array &$arr){

    foreach ($arr as $item)
    {
        switch ($item['type'])
        {
            case 'G' : 
                unset($item['s_id'],  $item['m_id']);
            break; 
            default : 
                unset($item['start'],  $item['end'], $item['time']);
            break;
        }

        foreach ($item["children"] as $c)
        {
            $child = array();
            getarray($child);
        }
    }
}

我怎样才能做到这一点?

感谢。

2 个答案:

答案 0 :(得分:1)

function filterMyArray($arr){
 if(isset($arr['type'] && $arr['type'] == 'G'){
   unset($arr['s_id']);
   unset($arr['m_id']);
 }
else if(isset($arr['type']){
  unset($arr['start']);
  unset($arr['end']);
  unset($arr['time']);
}
if(isset($arr['children'])
  $arr['children'] = filterMyArray($arr['children']);
return $arr;
}

这个函数没有经过测试,我是从内存中写的。 但严重的是,谷歌搜索这样的东西并没有伤害。你需要5分钟才能获得所需的所有知识......

答案 1 :(得分:0)

我自己解决了:

private function getarray(array &$arr){

        foreach ($arr as &$item)
        {
            switch ($item['type'])
            {
                case 'G' : 
                    unset($item['s_id'], $item['m_id']);
                    break; 

                default : 
                    unset($item['start'], $item['end'], $item['time']);
                    break;
            }
            if (is_array($item['children']) && sizeof($item['children']) > 0){
                getarray($item['children']);
            }
        }
    }

这很好用。我已经发布了这个答案,以帮助其他新手php并试图克服他们的问题。

对于嫉妒的人来说,这是一记耳光,他们没有帮助我,但问道,#34;你做了多久的尝试?"具有优越的复杂态度。我自己做了。