PHP数组权限导航器

时间:2015-10-07 13:22:28

标签: php arrays multidimensional-array

我有以下功能:

        function navigation_permission($permission) {
        array(
        1 => array(
            array('name' => 'members','onclick' => 'test11'),
            array('name' => 'members','onclick' => 'test12'),
            array('name' => 'members','onclick' => 'test13').
            ),
        2 => array(
        array('name' => 'members','onclick' => 'test21'),
        array('name' => 'members','onclick' => 'test22'),
        array('name' => 'members','onclick' => 'test23'),
        )

        5 => array(
        array('name' => 'members','onclick' => 'test34'),
        array('name' => 'members','onclick' => 'test35'),
        )
        );

        // RETURN
    }

$permission是0到6之间的数字。

如何让它能够根据数组中的权限和键值返回数组?

以下是输出的一些示例:

navigation_permission(6);

would return an array with all the other arrays, example:

    array(
    array('name' => 'members','onclick' => 'test11'),
 array('name' => 'members','onclick' => 'test12'),
 array('name' => 'members','onclick' => 'test13'),
 array('name' => 'members','onclick' => 'test21'),
 array('name' => 'members','onclick' => 'test22'),
 array('name' => 'members','onclick' => 'test23'),
 array('name' => 'members','onclick' => 'test34'),
 array('name' => 'members','onclick' => 'test35')
);

navigation_permission(3)将返回上一个示例中的所有内容,除了包含test34和test35的两个数组,原因5大于3。

navigation_permission(2)会在1和2中返回相同的内容。 navigation_permission(1)只返回1中的数组。

例二:

navigation_permission(3)

 array(
        array('name' => 'members','onclick' => 'test11'),
     array('name' => 'members','onclick' => 'test12'),
     array('name' => 'members','onclick' => 'test13'),
     array('name' => 'members','onclick' => 'test21'),
     array('name' => 'members','onclick' => 'test22'),
     array('name' => 'members','onclick' => 'test23'),
    );

问:如何让navigation_permission函数返回此类?

2 个答案:

答案 0 :(得分:0)

这将返回所有小于$permission

的数组
 function navigation_permission($permission) {
            $initialArray = array(
            1 => array(
                array('name' => 'members','onclick' => 'test11'),
                array('name' => 'members','onclick' => 'test12'),
                array('name' => 'members','onclick' => 'test13').
                ),
            2 => array(
            array('name' => 'members','onclick' => 'test21'),
            array('name' => 'members','onclick' => 'test22'),
            array('name' => 'members','onclick' => 'test23'),
            ),

            5 => array(
            array('name' => 'members','onclick' => 'test34'),
            array('name' => 'members','onclick' => 'test35'),
            )
            );
            $returnArr = array();
            for($i=1; $i <= $permission; $i++){
               foreach($initialArray[$i] as $row){

                $returnArr[] = $row;
               }
             }
            return $returnArr;
        }

答案 1 :(得分:-1)

你可以使用类似这个功能的东西。

function navigation_permission($permission)
{

    $res = [];

    switch ($permission)
    {
        case 6:
            $res = array_merge($res,[
                [ 'name' => 'members','onclick' => 'test11' ],
                [ 'name' => 'members','onclick' => 'test12' ],
                [ 'name' => 'members','onclick' => 'test13' ],
            ]);

        case 5:
            $res = array_merge($res,[
                    [ 'name' => 'members','onclick' => 'test11' ],
                    [ 'name' => 'members','onclick' => 'test12' ],
                    [ 'name' => 'members','onclick' => 'test13' ],
                ]);

        case 4:
            $res = array_merge($res,[
                    [ 'name' => 'members','onclick' => 'test11' ],
                    [ 'name' => 'members','onclick' => 'test12' ],
                    [ 'name' => 'members','onclick' => 'test13' ],
                ]);

        case 3:
            $res = array_merge($res,[
                    [ 'name' => 'members','onclick' => 'test11' ],
                    [ 'name' => 'members','onclick' => 'test12' ],
                    [ 'name' => 'members','onclick' => 'test13' ],
                ]);

        case 2:
            $res = array_merge($res,[
                    [ 'name' => 'members','onclick' => 'test11' ],
                    [ 'name' => 'members','onclick' => 'test12' ],
                    [ 'name' => 'members','onclick' => 'test13' ],
                ]);

        case 1:
            $res = array_merge($res,[
                    [ 'name' => 'members','onclick' => 'test11' ],
                    [ 'name' => 'members','onclick' => 'test12' ],
                    [ 'name' => 'members','onclick' => 'test13' ],
                ]);
        break;
    }

    return $res;

};

是的,6个案例中有5个案例的断言不存在,无法提供您需要的功能:
切换控制结构:Manual on php.net
关于stackoverflow的讨论:switch statement without break