用于创建新的多维数组的递归函数

时间:2011-09-08 21:38:23

标签: php arrays function recursion multidimensional-array

你好,我有一个如下所示的导航阵列。

我必须删除一些“durum”条件eq为0的节点。

请注意,如果它是父节点并且它等于0,则不会包含任何后代。 我必须使用下面的递归函数。但我无法成功。

谢谢。

function visibleCheck(&$array)
{
       $result = array();

        foreach ($array as $key => $value) {

            if($value['durum'] == 1)
            {       

                if(isset($value['children']))
                {


                    $this->visibleCheck($value['children']);

                }   
                $result[] = $value;
            }

        }
        return $result;
}


Array
(
[0] => Array
    (
        [id] => 391
        [name] => Ana Sayfa
        [link] => Ana-Sayfa
        [durum] => 1
        [visible] => 1
        [lft] => 2
        [rgt] => 3
        [lang_id] => 1
        [menu_id] => 10
        [slug] => comp_empty
        [user_level] => 0
        [page_id] => 
        [target] => _parent
        [nolink] => 0
        [default] => 1
        [usort] => 
        [search] => 0
    )

[1] => Array
    (
        [id] => 393
        [name] => Hakkımızda
        [link] => Hakkimizda
        [durum] => 1
        [visible] => 1
        [lft] => 6
        [rgt] => 7
        [lang_id] => 1
        [menu_id] => 10
        [slug] => comp_content
        [user_level] => 0
        [page_id] => 1
        [target] => _parent
        [nolink] => 0
        [default] => 0
        [usort] => 
        [search] => 0
    )

[2] => Array
    (
        [id] => 394
        [name] => Referanslar
        [link] => Referanslar
        [durum] => 1
        [visible] => 1
        [lft] => 8
        [rgt] => 25
        [lang_id] => 1
        [menu_id] => 10
        [slug] => comp_seperator
        [user_level] => 0
        [page_id] => 
        [target] => _parent
        [nolink] => 1
        [default] => 0
        [usort] => 
        [search] => 0
        [children] => Array
            (
                [0] => Array
                    (
                        [id] => 403
                        [name] => Teslim Edilen Projeler
                        [link] => Teslim-Edilen-Projeler
                        [durum] => 0
                        [visible] => 0
                        [lft] => 9
                        [rgt] => 20
                        [lang_id] => 1
                        [menu_id] => 10
                        [slug] => comp_content
                        [user_level] => 0
                        [page_id] => 33
                        [target] => _parent
                        [nolink] => 0
                        [default] => 0
                        [usort] => 
                        [search] => 0
                        [children] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 405
                                        [name] => Aegean - 30m
                                        [link] => Aegean-30m
                                        [durum] => 1
                                        [visible] => 1
                                        [lft] => 10
                                        [rgt] => 11
                                        [lang_id] => 1
                                        [menu_id] => 10
                                        [slug] => comp_content
                                        [user_level] => 0
                                        [page_id] => 11
                                        [target] => _parent
                                        [nolink] => 0
                                        [default] => 0
                                        [usort] => 
                                        [search] => 0
                                    )

                                [1] => Array
                                    (
                                        [id] => 406
                                        [name] => Caner IV - 40m.
                                        [link] => Caner-IV-40m
                                        [durum] => 1
                                        [visible] => 1
                                        [lft] => 12
                                        [rgt] => 13
                                        [lang_id] => 1
                                        [menu_id] => 10
                                        [slug] => comp_content
                                        [user_level] => 0
                                        [page_id] => 13
                                        [target] => _parent
                                        [nolink] => 0
                                        [default] => 0
                                        [usort] => 
                                        [search] => 0
                                    )

                                [2] => Array
                                    (
                                        [id] => 407
                                        [name] => Carpediem
                                        [link] => Carpediem
                                        [durum] => 1
                                        [visible] => 1
                                        [lft] => 14
                                        [rgt] => 15
                                        [lang_id] => 1
                                        [menu_id] => 10
                                        [slug] => comp_content
                                        [user_level] => 0
                                        [page_id] => 15
                                        [target] => _parent
                                        [nolink] => 0
                                        [default] => 0
                                        [usort] => 
                                        [search] => 0
                                    )

                                [3] => Array
                                    (
                                        [id] => 408
                                        [name] => Kaya Güneri V
                                        [link] => Kaya-Guneri-V
                                        [durum] => 1
                                        [visible] => 1
                                        [lft] => 16
                                        [rgt] => 17
                                        [lang_id] => 1
                                        [menu_id] => 10
                                        [slug] => comp_content
                                        [user_level] => 0
                                        [page_id] => 17
                                        [target] => _parent
                                        [nolink] => 0
                                        [default] => 0
                                        [usort] => 
                                        [search] => 0
                                    )

                                [4] => Array
                                    (
                                        [id] => 409
                                        [name] => Take it easier
                                        [link] => Take-it-easier
                                        [durum] => 1
                                        [visible] => 1
                                        [lft] => 18
                                        [rgt] => 19
                                        [lang_id] => 1
                                        [menu_id] => 10
                                        [slug] => comp_content
                                        [user_level] => 0
                                        [page_id] => 21
                                        [target] => _parent
                                        [nolink] => 0
                                        [default] => 0
                                        [usort] => 
                                        [search] => 0
                                    )

                            )

                    )

                [1] => Array
                    (
                        [id] => 404
                        [name] => İnşaa Halindeki Projeler
                        [link] => Insaa-Halindeki-Projeler
                        [durum] => 0
                        [visible] => 0
                        [lft] => 21
                        [rgt] => 24
                        [lang_id] => 1
                        [menu_id] => 10
                        [slug] => comp_content
                        [user_level] => 0
                        [page_id] => 35
                        [target] => _parent
                        [nolink] => 0
                        [default] => 0
                        [usort] => 
                        [search] => 0
                        [children] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 410
                                        [name] => SeaWide
                                        [link] => SeaWide
                                        [durum] => 1
                                        [visible] => 1
                                        [lft] => 22
                                        [rgt] => 23
                                        [lang_id] => 1
                                        [menu_id] => 10
                                        [slug] => comp_content
                                        [user_level] => 0
                                        [page_id] => 19
                                        [target] => _parent
                                        [nolink] => 0
                                        [default] => 0
                                        [usort] => 
                                        [search] => 0
                                    )

                            )

                    )

            )

    )

[3] => Array
    (
        [id] => 412
        [name] => Yat Bakım & Onarım
        [link] => Yat-Bakim-Onarim
        [durum] => 1
        [visible] => 1
        [lft] => 26
        [rgt] => 45
        [lang_id] => 1
        [menu_id] => 10
        [slug] => comp_content
        [user_level] => 0
        [page_id] => 29
        [target] => _parent
        [nolink] => 0
        [default] => 0
        [usort] => 
        [search] => 0
        [children] => Array
            (
                [0] => Array
                    (
                        [id] => 417
                        [name] => Ahşap
                        [link] => Ahsap
                        [durum] => 0
                        [visible] => 0
                        [lft] => 27
                        [rgt] => 32
                        [lang_id] => 1
                        [menu_id] => 10
                        [slug] => comp_empty
                        [user_level] => 0
                        [page_id] => 
                        [target] => _parent
                        [nolink] => 0
                        [default] => 0
                        [usort] => 
                        [search] => 0
                        [children] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 420
                                        [name] => Gövde
                                        [link] => Govde
                                        [durum] => 1
                                        [visible] => 1
                                        [lft] => 28
                                        [rgt] => 29
                                        [lang_id] => 1
                                        [menu_id] => 10
                                        [slug] => comp_empty
                                        [user_level] => 0
                                        [page_id] => 
                                        [target] => _parent
                                        [nolink] => 0
                                        [default] => 0
                                        [usort] => 
                                        [search] => 0
                                    )

                                [1] => Array
                                    (
                                        [id] => 421
                                        [name] => İç tasarım
                                        [link] => Ic-tasarim
                                        [durum] => 1
                                        [visible] => 1
                                        [lft] => 30
                                        [rgt] => 31
                                        [lang_id] => 1
                                        [menu_id] => 10
                                        [slug] => comp_empty
                                        [user_level] => 0
                                        [page_id] => 
                                        [target] => _parent
                                        [nolink] => 0
                                        [default] => 0
                                        [usort] => 
                                        [search] => 0
                                    )

                            )

                    )

                [1] => Array
                    (
                        [id] => 418
                        [name] => Komposit
                        [link] => Komposit
                        [durum] => 0
                        [visible] => 1
                        [lft] => 33
                        [rgt] => 38
                        [lang_id] => 1
                        [menu_id] => 10
                        [slug] => comp_empty
                        [user_level] => 0
                        [page_id] => 
                        [target] => _parent
                        [nolink] => 0
                        [default] => 0
                        [usort] => 
                        [search] => 0
                        [children] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 422
                                        [name] => Gövde
                                        [link] => Govde
                                        [durum] => 1
                                        [visible] => 1
                                        [lft] => 34
                                        [rgt] => 35
                                        [lang_id] => 1
                                        [menu_id] => 10
                                        [slug] => comp_empty
                                        [user_level] => 0
                                        [page_id] => 
                                        [target] => _parent
                                        [nolink] => 0
                                        [default] => 0
                                        [usort] => 
                                        [search] => 0
                                    )

                                [1] => Array
                                    (
                                        [id] => 423
                                        [name] => İç tsarım
                                        [link] => Ic-tsarim
                                        [durum] => 1
                                        [visible] => 1
                                        [lft] => 36
                                        [rgt] => 37
                                        [lang_id] => 1
                                        [menu_id] => 10
                                        [slug] => comp_empty
                                        [user_level] => 0
                                        [page_id] => 
                                        [target] => _parent
                                        [nolink] => 0
                                        [default] => 0
                                        [usort] => 
                                        [search] => 0
                                    )

                            )

                    )

                [2] => Array
                    (
                        [id] => 419
                        [name] => Çelik
                        [link] => Celik
                        [durum] => 0
                        [visible] => 1
                        [lft] => 39
                        [rgt] => 44
                        [lang_id] => 1
                        [menu_id] => 10
                        [slug] => comp_empty
                        [user_level] => 0
                        [page_id] => 
                        [target] => _parent
                        [nolink] => 0
                        [default] => 0
                        [usort] => 
                        [search] => 0
                        [children] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 424
                                        [name] => Gövde
                                        [link] => Govde
                                        [durum] => 1
                                        [visible] => 1
                                        [lft] => 40
                                        [rgt] => 41
                                        [lang_id] => 1
                                        [menu_id] => 10
                                        [slug] => comp_empty
                                        [user_level] => 0
                                        [page_id] => 
                                        [target] => _parent
                                        [nolink] => 0
                                        [default] => 0
                                        [usort] => 
                                        [search] => 0
                                    )

                                [1] => Array
                                    (
                                        [id] => 425
                                        [name] => İç tasarım
                                        [link] => Ic-tasarim
                                        [durum] => 1
                                        [visible] => 1
                                        [lft] => 42
                                        [rgt] => 43
                                        [lang_id] => 1
                                        [menu_id] => 10
                                        [slug] => comp_empty
                                        [user_level] => 0
                                        [page_id] => 
                                        [target] => _parent
                                        [nolink] => 0
                                        [default] => 0
                                        [usort] => 
                                        [search] => 0
                                    )

                            )

                    )

            )

    )

[4] => Array
    (
        [id] => 396
        [name] => Aksesuarlar
        [link] => Aksesuarlar
        [durum] => 1
        [visible] => 1
        [lft] => 46
        [rgt] => 47
        [lang_id] => 1
        [menu_id] => 10
        [slug] => comp_content
        [user_level] => 0
        [page_id] => 5
        [target] => _parent
        [nolink] => 0
        [default] => 0
        [usort] => 
        [search] => 0
    )

[5] => Array
    (
        [id] => 395
        [name] => Brokerlik
        [link] => Brokerlik
        [durum] => 1
        [visible] => 1
        [lft] => 48
        [rgt] => 49
        [lang_id] => 1
        [menu_id] => 10
        [slug] => comp_category_blog
        [user_level] => 0
        [page_id] => 
        [target] => _parent
        [nolink] => 0
        [default] => 0
        [usort] => 
        [search] => 0
    )

[6] => Array
    (
        [id] => 397
        [name] => İletişim
        [link] => Iletisim
        [durum] => 1
        [visible] => 1
        [lft] => 50
        [rgt] => 53
        [lang_id] => 1
        [menu_id] => 10
        [slug] => comp_contact
        [user_level] => 0
        [page_id] => 
        [target] => _parent
        [nolink] => 0
        [default] => 0
        [usort] => 
        [search] => 0
        [children] => Array
            (
                [0] => Array
                    (
                        [id] => 427
                        [name] => deneme
                        [link] => deneme
                        [durum] => 0
                        [visible] => 1
                        [lft] => 51
                        [rgt] => 52
                        [lang_id] => 1
                        [menu_id] => 10
                        [slug] => comp_content
                        [user_level] => 0
                        [page_id] => 37
                        [target] => _parent
                        [nolink] => 0
                        [default] => 0
                        [usort] => 
                        [search] => 0
                    )

            )

    )

1 个答案:

答案 0 :(得分:0)

试试这个:

function visibleCheck($test) {
    $result = array();
    foreach($test as $key => $value) {
        if(isset($value['durum']) && $value['durum'] != 0) {
            if(isset($value['children'])) {
                $value['children']= visibleCheck($value['children']);
            }
            $result[$key] = $value;
        }
    }
    return $result;
}

$test = array();
$test[0] = array();
$test[1] = array();
$test[2] = array();
$test[3] = array();
$test[4] = array();

$test[0]["durum"]=0;
$test[1]["durum"]=1;
$test[2]["durum"]=1;
$test[3]["durum"]=0;
$test[4]["durum"]=1;

$test[0]["children"]= array();
$test[1]["children"]= array(
    0=> array(
        'durum'=>1,
        'some'=>'some more',
        'children'=>array(
            0=>array(
                'durum'=>0,
                'some'=>'some more',
                'children'=>array(
                    0=>array(),
                    1=>array()
                )
            ),
            1=>array(
                'durum'=>1,
                'some'=>'some more',
                'children'=>array(
                    0=>array(),
                    1=>array()
                )
            ),
            2=>array(
                'durum'=>1,
                'some'=>'some more',
                'children'=>array(
                    0=>array(
                        0=>array(
                            'durum'=>1,
                            'some'=>'some more',
                            'children'=>array(
                                0=>array(),
                                1=>array()
                            )
                        ),
                        1=>array(
                            'durum'=>0,
                            'some'=>'some more',
                            'children'=>array(
                                0=>array(),
                                1=>array()
                            )
                        ),
                    ),
                    1=>array()
                )
            ),
        ),
    ),
    1=> array(
        'durum'=>0,
        'some'=>'some more',
        'children'=>array(
            0=>array(
                'durum'=>0,
                'some'=>'some more',
                'children'=>array(
                    0=>array(),
                    1=>array()
                )
            ),
        ),
    )
);
$test[2]["children"]= array();
$test[3]["children"]= array();
$test[4]["children"]= array();

echo "Before: <pre>";
echo print_r($test);
echo "</pre>After<pre>";
echo print_r(visibleCheck($test));