简体   繁体   中英

Foreach $_POST - how to increase array pointer to the next element

I'm looping through the $_POST array (generated form elements) and I need to get the keys and values of the next elements in my foreach loop.

Edit: The name of the elements for the answers (ie canbeanynameABC and canbeanynameXYZ) are always unknown.

Here is an example array:

Array ( 
[inputid] => 87 [inputoutputtype] => radio [canbeanynameABC] => radio answer 2 
[inputid] => 88 [inputoutputtype] => radio [canbeanynameXYZ] => radio answer 4 ) 

My code here

foreach ($_POST as $key => $value) {

switch ($key) {

    case "inputid":
            echo "<br/>Value of input_id : " . $value;      
        next($_POST);       
        echo "<br/>Value of inputoutputtype : " . $value;
        next($_POST);       
        echo "<br/>Value of answer : " . $value;        
        break;
}
}

I thought that by doing next($_POST), the pointer would now be positioned on the next key/value.

Apparently it doesn't work, i'm getting the following displays:

Value of input_id : 87
Value of input_output_type : 87
Value of answer : 87

Any help would be greatly appreciated. Thanks

Edit: It was suggested that I use arrays to organize my form elements/values. I still can't figure out how to use arrays so that each answer (value) returned from the form, I actually get three values (answer, inputid, inputoutputtype)

Edit: @CBroe I spent the afternoon trying to figure out how to use arrays in $_POST. The form can contain any number of elements, and these elements can be text, radio, select/option, checkbox (can have more than one value returned). Each "value" I get from this form must be associated with an "inputid" and an "inputoutputtype". These form and elements are generated using php, so i'm trying to build a php form handler that will read any numbers of elements and types. The generator is creating unique names for each element so that values don't get overwritten. I'm trying to figure out how to integrate arrays into the generator, but not sure if I'll be able to assign them a row number (ie [0], [1]..).. maybe I'm just not seeing how arrays would work for my situation.


Update : Ok, now i'm trying to modify my php form handler to deal with elements that could have any names. I need to be able to read id, type and value (3 different values) from element filled out in the form.

I'm playing around with the form (even though its entirely generated), but not sure what to name my elements

<input type='hidden' name='inputradio[inputid]' value='1'>
<input type='hidden' name='inputradio[inputoutputtype]' value='radio'>
<input type='radio' name='inputradio[output]' value='Radio answer 1'>

<input type='hidden' name='inputradio[inputid]' value='2'>
<input type='hidden' name='inputradio[inputoutputtype]' value='radio'>
<input type='radio' name='inputradio[output]' value='Radio answer 2'>

<input type='hidden' name='inputtext1[inputid]' value='3'>
<input type='hidden' name='inputtext1[inputoutputtype]' value='text'>
<input type='text' name='inputtext1[output]' value='Text answer 1'>

<input type='hidden' name='inputtext2[inputid]' value='4'>
<input type='hidden' name='inputtext2[inputoutputtype]' value='text'>
<input type='text' name='inputtext2[output]' value='Text answer 2'>

Should I change the title of this post if the direction changed a but?

Thanks C

There are only two properties to an entry in $_POST, the key and the value. I don't know where you got your example array, but it doesn't work like that. So if I had a radio button group with all sharing the same name, I would get the name->value pair.

If I created the following form and I submitted it with value a selected in the radio button group:

<form method="POST" >
    <input type="text" name="textbox" value="test"><br/>
    a.<input type="radio" name="rd" value="a"><br/>
    b.<input type="radio" name="rd" value="b"><br/>
    c.<input type="radio" name="rd" value="c"><br/>
    <input type="submit" value="submit">
</form>

my $_POST would contain the following values:

Array
(
    [textbox] => test
    [rd] => a
)

if I tried to step through my array, I would do so as follows:

for ($_POST as $key->$value) {
    echo $key . " = " . $value . "\n";
}

Which would give me the following output:

textbox = test
rd = a

Can you tell me what problem you are trying to solve, since it seems you are approaching this from the point of view of some other language or framework, or possibly you are looking to work with some other array in PHP that is not $_POST?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM