Solved

# Looping Through 2D Array

Posted on 2008-10-16
542 Views
The below is my code.

What I need to do is loop though each value of the array, and run a test against that value of the array, for example if (\$checked[0][0] == check) then do something... my problem is in how to handle the loop.

1. I need to step through a 2D array.. so the values are like \$checked[0][0] through \$checked[9][9] I'm not sure a for each will do this...

2. Secondly as I go through each loop I need to make sure the cell of the array that I am testing is increasing as I go up... so the first time through  I am testing \$checked[0][0] the second \$checked[0][1], ect until 9,9

3. I will sometimes be changing the value of the current array cell that I am testing for example if \$checked[0][0] == check and \$checked[0][1] == checked { \$checked[0][0] = unchecked } but I need to store all the changes and not pass them to the revised grid until after all the current cells have been tested.

``````<?

if (isset(\$_POST['submit'])) {

// \$_POST['cb'] contains the array with selected checkboxes.

\$checked = \$_POST['cb'];

foreach (\$checked as \$loop) {if (\$checked[0][0] == check) {

echo "test";

}}

echo "<pre>";

print_r(\$checked);

echo "</pre>";

}

?>

<form method='post'>

<?

for (\$y = 0; \$y < 9; \$y++) {

for (\$x = 0; \$x < 9; \$x++) {

echo "<input type='checkbox' name='cb[" . \$y . "][" . \$x. "]' value='check' ";

if (isset(\$checked[\$y][\$x])) {

echo "checked ";

}

echo "/>\n";

}

echo "<br />";

}

echo "<input type='submit' action='".\$_SERVER['PHP_SELF']."' name='submit'>\n";

?>

<input type="submit" name="submit" />

</form>
``````
0
Question by:cstormer

LVL 9

Accepted Solution

Shahzad Fateh Ali earned 500 total points
ID: 22737978
Hi,

Try this.

``````<?

\$max_rows = 9;

\$max_cols = 9;

if (isset(\$_POST['submit'])) {

// \$_POST['cb'] contains the array with selected checkboxes.

\$checked = \$_POST['cb'];

for (\$y = 0; \$y < \$max_rows; \$y++) {

for (\$x = 0; \$x < \$max_cols; \$x++) {

// you code for modification here

echo (\$y+1).",".(\$x+1)." is ";

echo (\$checked[\$y][\$x] === 'check')?"checked<br>":"unchecked<br>";

}

}

}

?>

<form method='post'>

<?

for (\$y = 0; \$y < \$max_rows; \$y++) {

for (\$x = 0; \$x < \$max_cols; \$x++) {

echo "<input type='checkbox' name='cb[" . \$y . "][" . \$x. "]' value='check' ";

if (isset(\$checked[\$y][\$x])) {

echo "checked ";

}

echo "/>\n";

}

echo "<br />";

}

?>

<input type="submit" name="submit" />

</form>
``````
0

Author Closing Comment

ID: 31507034
Perfect
0

## Featured Post

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …