【问题标题】:match array of posted values to an array of functions in php将发布的值数组与php中的函数数组匹配
【发布时间】:2013-03-17 13:03:37
【问题描述】:

我有一个基本的逻辑问题。我的目标是从表单中取出选定的复选框,将它们作为数组提交,然后将值与函数数组匹配。我试图找到一种将所选输入与函数数组匹配的方法,这样我就不会以 if {} elseif{} 语句的页面结束,以涵盖越来越多的选项。

这是我如何设置包含一组复选框的网络表单的示例。我正在使用“[]”将选定的值作为数组传递。

网络表单:

<input type="checkbox" name="action[]" value="open" checked>open</input>
<input type="checkbox" name="action[]" value="close">close</input>
<input type="checkbox" name="action[]" value="lift">lift</input>
<input type="checkbox" name="action[]" value="drop">drop</input>
<input type="checkbox" name="action[]" value="destroy">destroy</input>
<input type="checkbox" name="action[]" value="create">create</input>

php 后处理程序

$o = open();
$c = create();
$l = lift();
$d = drop();
$de = destroy();
$cr = create();

$mysvcs = array($o, $c, $l, $d, $de, $cr);


//stuck here
$action = $_POST['action'];
$postsvc = implode(",", $action );  

$req = strstr($mysvcs, $postsvc);

?>

我感觉我可能以完全错误的方式处理此问题,并希望社区提供任何指导。

非常感谢

-澳洲恶魔

【问题讨论】:

  • 你打算用输入做什么?您想验证收到的每个值是您可接受的参数之一吗?为此,您应该使用in_array() - 循环$_POST['action'] 并验证每个操作是您接受的操作数组中的in_array(),它们只是像array('open','close','lift','drop'...) 这样的字符串

标签: php arrays forms post checkbox


【解决方案1】:

如果你想执行检查的功能,下面的代码应该可以工作:

function open() {} //your open declaration 
function close() {} //your close declaration 
function lift() {} //your lift declaration 
//all other declarations

foreach($_POST['action'] as $a){
    $a();//execute matching function 
}

【讨论】:

  • 工作得很好,因为没有考虑到这一点而自责:)。非常感谢!
猜你喜欢
  • 2018-04-24
  • 2011-07-22
  • 2012-06-15
  • 2021-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多