我有以下功能:
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'),
);
Q:如何使navigation_permission函数返回这种类型?
这将返回所有小于$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;
}
您可以使用下面的函数。
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个没有break语句来提供你需要的功能:
Switch控制结构:Manual on php.net
关于stackoverflow: switch不带break语句的讨论