SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    FBI secret agent digitman's Avatar
    Join Date
    Sep 2004
    Location
    Work
    Posts
    697
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    passing arrays in functions

    Hey guys.I have this function in my code:

    Code:
    function get_submitted_data($form) {
    //This function retreives the data submitted from a form and returns it in an array.
    for ($x=0;$x<count($form);$x++) {
    $value[]=$_POST['{$form[$x]}'];
    }
    return $value;
    }
    
    //And I use the following code to attempt to retreive the data:
    
    $form[]="name";
    $form[]="email";
    $form[]="username";
    $form[]="password";
    list($value)=get_submitted_data($form);
    But it always returns an empty value.I think it must have something with this line:
    Code:
    $value[]=$_POST['{$form[$x]}'];
    What should I do?

  2. #2
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should really use foreach, it's much easier.

    PHP Code:
    function get_submitted_data($form)
    {
    //This function retreives the data submitted from a form and returns it in an array.
    $value = array();
    foreach(
    $form AS $k=>$v)
    {
        
    $value[] = $_POST[$v];
    }
    return 
    $value;
    }

    //And I use the following code to attempt to retreive the data:

    $form = array();
    $form[] = 'name';
    $form[] = 'email';
    $form[] = 'username';
    $form[] = 'password';
    print_r(get_submitted_data($form)); 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •