If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 3 of 3
  1. #1
    Senior Member
    Join Date
    Apr 2005
    Hatfield, England
    Member #
    This should be simple enough but I am really struggling with it.

    I want to process the first value of the $_POST array.

    The name of the $_POST field in question is set with a variable, this means I do not know what it is until I process it.

    Does anyone know how to get $_POST[0]?


  3. #2
    Senior Member
    Join Date
    Jun 2005
    Atlanta, GA
    Member #
    1 times
    There's no way to just get `the first' element in $_POST, since POST requests are not really ordered (though typically things are serialized in a relatively ordered way).

    The only real way to do this would be to use something like [phpfunction]array_pop[/phpfunction] to pop the first element in $_POST off of the list. However, again, there's no guarantee as to whether that'll be what you consider the `first' element or not.

    The other way is to use [phpfunction]each[/phpfunction], something like this:
    PHP Code:
    list($fieldName$value) = each($_POST); 
    Basically, you shouldn't be using a dynamically generated field name. Your field names should be easily referencable. If you have a dynamically generated name to keep track of additional information, put that information in a hidden field or something, or as another part of the value in your item.

    You can, for example, do something like this:
    PHP Code:
    <input type="hidden" name="person[type]" value="<?= $user_type ?>" />
    <label for="person_name">Name: </label><input type="text" id="person_name" name="person[name]" />
    And then you can extract that information:
    PHP Code:
    $person $_POST['person']; // has $person['type'] and $person['name'] 

  4. #3
    Senior Member Steax's Avatar
    Join Date
    Dec 2006
    Bandung, Indonesia
    Member #
    May I ask the simple question of why you need the first element from the POST superglobal? I've been thinking for hours and still have no idea why you'd need to do that.
    Note on code: If I give code, please note that it is simply sample code to demonstrate an effect. It is not meant to be used as-is; that is the programmer's job. I am not responsible to give you support or be held liable for anything that happens when using my code.

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
All times are GMT -6. The time now is 02:59 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2022 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: