通过html名称表单检索php变量


Retrieve php variable via html name form

我只需要知道php变量的html输入名称就可以检索它。

例如:我知道输入的名称是admin[username],所以我想获得变量$_POST['admin']['username]

我需要这个来对输入进行某种验证。换句话说,我在显示表单时保存html输入名称,但只在下一个服务器请求时测试它,所以我并不总是知道php变量名称是什么,因为它可以通过javascript修改,或者如果html输入名称是admin[],我可以有数组

感谢

$postdata = get_associative_array_from_post_data($post['admin']);
function get_associative_array_from_post_data($array){
    if(isAssoc($array)){
        /*foreach($array as $key=>$value){
            $your_key = $key;
            $your_value = $value;
        }*/
        return $array;
    }
    else{ //if the post array has no keys, search for arrays within this 
        $key_collection=array();
        foreach($array as $sub_array){
            foreach($sub_array as $key=>$value){
                $key_collection[$key][] = $value;
            }
        }
        return $key_collection;
    }
}    

function isAssoc($arr)
{
    return array_keys($arr) !== range(0, count($arr) - 1);
}

如果你发布:

admin[用户名]='test'和admin[电子邮件]='test@example.com'

你会得到

$postdata = array('username'=>'test', 'email'=>'test@example.com');

如果您发布:admin[][username]='test'和admin[][username]='test2'admin[][电子邮件]='test@example.com'和admin[][电子邮件]='test2@example.com'

您将获得:

$postdata = array(
                      'username'=>array('test', 'test2'),
                      'email'=>array('test@example.com', 'test2@example.com')
                 );