1

I need to retrieve an php variable only by knowing it's html input name.

Such as: I know the input's name is admin[username] so I want to get the variable $_POST['admin']['username]

I need this for some sort of validations of the inputs. In other word, I save the html input name when I display the form, but only test it at the next server request, so I don't always know what will be the php variable name, since it can be modified by javascript or I can have array such if the html input name is admin[]

Thank you

11
  • You are looking for $_POST['admin'][] array elements? Commented Jul 11, 2012 at 13:44
  • your html is as follows? <input type=text name="admin[username]" /> ? Commented Jul 11, 2012 at 13:44
  • @DainisAbols If I pass the string "admin[]" I will be looking for $_POST['admin'][] yes, but it can be simple "admin", "admin[username]", "admin[username][]" or "admin[username][][test]" of course, this is only examples and the names don't really means anything Commented Jul 11, 2012 at 13:47
  • 2
    This is rather confusing, could you stop telling us what you think you should do about a problem we still don't understand. But clearly explain what you want to do and what code you currently have. Commented Jul 11, 2012 at 14:09
  • 1
    No @DainisAbols asked you to show you what you have built so far, exactly the same thing I am asking. And some underlying clarification wouldn't hurt no. What I currently understand is you post a form values with dynamic keynames within an array. This could be a associative array(admin[username][test]) but you seem to have some difficulty when this array does not have a key (admin[username][]). Something like that. Commented Jul 11, 2012 at 14:19

1 Answer 1

2
$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);
}

If you post:

admin[username] = 'test' and admin[email] = '[email protected]'

you will get

$postdata = array('username'=>'test', 'email'=>'[email protected]');

If you post : admin[][username] = 'test' and admin[][username] = 'test2' admin[][email] = '[email protected]' and admin[][email] = '[email protected]'

You will get:

$postdata = array(
                      'username'=>array('test', 'test2'),
                      'email'=>array('[email protected]', '[email protected]')
                 );
Sign up to request clarification or add additional context in comments.

1 Comment

I like your idea to validate if this is an associate array, this will certainly help me Thx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.