Loop through $_POST values

I have a very simple form on my website.

I run this PHP to loop the form contents:

echo '<pre>';
var_dump($_POST);
echo '</pre>';

$i = 0;

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

    $i++;
    echo $value[$i]['row_id'];

}

I get the below:

array(1) {
  ["data"]=> array(2) {
            [1]=>  array(2) {
                ["row_id"]=> string(5) "33714"
                ["sport"]=> string(8) "swimming"
            }
            [2]=> array(2) {
                ["row_id"]=> string(5) "33715"
                ["sport"]=> string(8) "football"
            }
  }
}

33714

My PHP only echoes the first row id 33714 instead of both rows.

I feel I’m missing something obvious here.

>Solution :

Simply put, you need to loop the items of $_POST['data'] instead of just $_POST

echo '<pre>';
var_dump($_POST);
echo '</pre>';

foreach($_POST['data'] as $key => $item)
{
    echo $key.':'. $item['row_id'];

}

Leave a Reply