Loop through a POST array
Asked Answered
M

6

16

I need to loop through a post array and submit it.

#stuff 1
<input type="text" id="stuff" name="stuff[]" />
<input type="text" id="more_stuff" name="more_stuff[]" />
#stuff 2
<input type="text" id="stuff" name="stuff[]" />
<input type="text" id="more_stuff" name="more_stuff[]" />

But I don't know where to start.

Medullary answered 21/4, 2012 at 20:33 Comment(4)
Same way you loop through every other array you've come across...Lugo
Some example code would be nice.Chignon
Read some basic documentation, such as php.net/manual/en/language.variables.external.php and php.net/manual/en/control-structures.foreach.php.Grijalva
This question Needs More Focus. We don't know your final expectation from "submitting it".Almita
S
41

This is how you would do it:

foreach( $_POST as $stuff ) {
    if( is_array( $stuff ) ) {
        foreach( $stuff as $thing ) {
            echo $thing;
        }
    } else {
        echo $stuff;
    }
}

This looks after both variables and arrays passed in $_POST.

Scarlatina answered 21/4, 2012 at 20:37 Comment(3)
problem is what about more_stuff[]?Medullary
print_r($_POST) is much easier. If you really want, you can even throw it between some <pre> tagsFrag
nice solution but theres a semicolon missing after the first echo :PAlyss
T
30

Likely, you'll also need the values of each form element, such as the value selected from a dropdown or checkbox.

 foreach( $_POST as $stuff => $val ) {
     if( is_array( $stuff ) ) {
         foreach( $stuff as $thing) {
             echo $thing;
         }
     } else {
         echo $stuff;
         echo $val;
     }
 }
Thigmotaxis answered 16/9, 2012 at 15:23 Comment(0)
K
7
for ($i = 0; $i < count($_POST['NAME']); $i++)
{
   echo $_POST['NAME'][$i];
}

Or

foreach ($_POST['NAME'] as $value)
{
    echo $value;
}

Replace NAME with element name eg stuff or more_stuff

Kendo answered 21/4, 2012 at 20:37 Comment(1)
$i < count($_POST['NAME']); would be more correct, no?Gorton
A
3

I have adapted the accepted answer and converted it into a function that can do nth arrays and to include the keys of the array.

function LoopThrough($array) {
    foreach($array as $key => $val) {
        if (is_array($key))
            LoopThrough($key);
        else 
            echo "{$key} - {$val} <br>";
    }
}

LoopThrough($_POST);

Hope it helps someone.

Adelina answered 26/10, 2018 at 8:32 Comment(0)
C
2

You can use array_walk_recursive and anonymous function, eg:

$sweet = array('a' => 'apple', 'b' => 'banana');
$fruits = array('sweet' => $sweet, 'sour' => 'lemon');
array_walk_recursive($fruits,function ($item, $key){
    echo "$key holds $item <br/>\n";
});

follows this answer version:

array_walk_recursive($_POST,function ($item, $key){
    echo "$key holds $item <br/>\n";
});
Carpio answered 17/1, 2014 at 20:18 Comment(0)
R
1

For some reason I lost my index names using the posted answers. Therefore I had to loop them like this:

foreach($_POST as $i => $stuff) {
  var_dump($i);
  var_dump($stuff);
  echo "<br>";
}
Redtop answered 31/5, 2018 at 22:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.