PHP how to loop through a post array

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


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;
     }
 }

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.

Tags:

Php

Post