Jump to content

Looping through multiple arays


kenoli

Recommended Posts

I am trying to capture the data from a form with several fields in a way that I can update a number of database records, each with several fields, from one form submittal.  I can capture the data from one of the form inputs like this:

 

<?php
if ($_POST['Submit']) {
   foreach($_POST['test'] as $key => $value) {
   echo $value . '<br/>';
   }
}
?>

<form name="form1" method="post" action="<?=$_SERVER['PHP_SELF'] ?>">
  Test:   <input type="text" name="test[]" id="textfield"><br>
  When: <input type="text" name="when[]" id="textfield"><br>
  Test:   <input type="text" name="test[]" id="textfield"><br>
  When: <input type="text" name="when[]" id="textfield"><br>
  Test:   <input type="text" name="test[]" id="textfield"><br>
  When: <input type="text" name="when[]" id="textfield"><br>
  Test:   <input type="text" name="test[]" id="textfield"><br>
  When: <input type="text" name="when[]" id="textfield"><br>
  Test:   <input type="text" name="test[]" id="textfield"><br>
  <input name="Submit" type="Submit">
</form>

 

Is there a way I can also capture the data from $_POST['when'] or even more similar field arrays?

 

I'm sure there is a way to do this but can't see it.  I would greatly appreciate help figuring this out.

 

Thanks,

 

--Kenoli

 

(edited by kenrbnsn to add


tags)

Link to comment
https://forums.phpfreaks.com/topic/108474-looping-through-multiple-arays/
Share on other sites

Sure. The $_POST array in PHP can handle javascript arrays of post variables. I tried this code:

<?php

if (count($_POST) > 0) {
echo '<pre>';
print_r($_POST);
echo '</pre>';
}
?>

<form name="form1" method="post">
  Test:   <input type="text" name="test[]" id="textfield">

  When: <input type="text" name="when[]" id="textfield">

  Test:   <input type="text" name="test[]" id="textfield">

  When: <input type="text" name="when[]" id="textfield">

  Test:   <input type="text" name="test[]" id="textfield">

  When: <input type="text" name="when[]" id="textfield">

  Test:   <input type="text" name="test[]" id="textfield">

  When: <input type="text" name="when[]" id="textfield">

  Test:   <input type="text" name="test[]" id="textfield">

  <input name="Submit" type="Submit">
</form>

 

And got this output after entering numbers 1-9 in left to right across the form fields:

Array
(
    [test] => Array
        (
            [0] => 1
            [1] => 3
            [2] => 5
            [3] => 7
            [4] => 9
        )

    [when] => Array
        (
            [0] => 2
            [1] => 4
            [2] => 6
            [3] => 8
        )

    [submit] => Submit Query
)

 

b.gif

 

You can loop through each $_POST item and then loop through its own array:

<?php
foreach ($_POST as $key => $val) {
if (is_array($val)) {
	foreach ($val as $k => $v) {
		echo "$k => $v<br>";
	}
} else {
	echo "$key => $val<br>";
}
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.