jim dif
jim dif

Reputation: 641

Getting at array elements in PHP

I have the following code:

<?php
if ( isset($_POST['submit']) )
{
    // print_r($_POST['row']); // Works perfectly
    echo $_POST['row[1][2]'];
}
?>


<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php 
$num_of_days_in_month=3;
for($i = 1; $i<= $num_of_days_in_month; $i ++) {
for($j = 1; $j <= 4; $j++) {
echo "<input type=\"text\" name=\"row[$i][$j]\" />";
if ($j ==4) {
    echo "<br>";
}
}
}
?>
<input type="submit" name="submit" />
</form>

I am trying to echo row 1, column 2 and the output is blank.

Anyone know why?

Thanks, Jim

Upvotes: 0

Views: 51

Answers (2)

Shakti Singh
Shakti Singh

Reputation: 86396

Try this

echo $_POST['row'][1][2];

Upvotes: 2

Mr. Alien
Mr. Alien

Reputation: 157334

Your indexing is wrong, try this

<?php
if ( isset($_POST['submit']) )
{
    echo $_POST['row'][1];
    echo $_POST['row'][2];
}
?>

Or

<?php
if ( isset($_POST['submit']) )
{
    echo $_POST['row'][1][2];
}
?>

Upvotes: 1

Related Questions