Solved

Looping Through 2D Array

Posted on 2008-10-16
2
544 Views
Last Modified: 2013-12-13
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.

Please help...
<?
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>

Open in new window

0
Comment
Question by:cstormer
2 Comments
 
LVL 9

Accepted Solution

by:
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>

Open in new window

0
 

Author Closing Comment

by:cstormer
ID: 31507034
Perfect
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP installation issues 11 60
PHP Parse error: syntax error, unexpected '[' 1 24
ignore other .htaccess 2 45
PHP Syntax Error 4 27
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question