vote up 2 vote down
star

Hi all, I have the following code:

        <tr>
      <td width="60%"><dl>
          <dt>Full Name</dt>
          <dd>
            <input name="fullname[]" type="text" class="txt w90" id="fullname[]" value="<?php echo $value; ?>" />
          </dd>
        </dl></td>
      <td width="30%"><dl>
          <dt>Job Title</dt>
          <dd>
            <input name="job_title[]" type="text" class="txt w90" id="job_title[]" value="<?php echo $value2; ?>" />
          </dd>
        </dl></td></tr>

Let assume that I have several rows of the above code. How do I iterate and get the value for arrays $_POST['fullname'] and $_POST['job_title']?

offensive?
add comment

8 Answers:

vote up 0 vote down

You could also iterate over the $_POST array like this

<?php
$entryCount = count($_POST['fullname']); // do not put your count() in the loop.
for ($n = 0; $n < $entryCount; $n++) {
   $incommingValues[$n]['fullname']  = $_POST['fullname'][$n];
   $incommingValues[$n]['job_title'] = $_POST['job_title'][$n];
}
?>

this would create an array that looks like this

$incommingValues = array (
   [0] = array(
      ['fullname'] = 'John';
      ['job_title'] = 'engineer';
   )
   [1] = array(
      ['fullname'] = 'Jack';
      ['job_title'] = 'marketeer';
   )
   [n] = array(
      ['fullname'] = 'Elise';
      ['job_title'] = 'driver';
   ) 
);

The big advantage is that you now have all your values together grouped by index number.

this makes for nice and clean code when you want them back:

<?php
$incommingValuesCount = count($incommingValues);
for ($n = 0; $n < $incommingValuesCount; $n++) {
   echo $incommingValues[$n]['fullname'];
   echo $incommingValues[$n]['job_title'];
}
?>

PLEASE NOTE:
echoing back data the user provided without any filtering is a very dangerous idea!
Search stackoverflow for XSS attack.

link|offensive?
add comment
vote up 0 vote down

Its pretty close to what I want to do. However I still need access to the original index number because let just say the row would be like this under some circumstances:

<tr>..
$_POST['fullname'][0] .... $_POST['job_title'][0]
$_POST['fullname'][2] .... $_POST['job_title'][2]
$_POST['fullname'][3] .... $_POST['job_title'][3]
..</tr>

By resetting the pointers obviously I would lose the value for $_POST['fullname'][3], am I right?

link|offensive?
comments (3)
vote up 1 vote down

If I understand you correctly, you have 2 arrays that you basically wish to iterate over in parallel.

Something like the below may work for you. Instead of $a1, and $a2 you would use $_POST['fullname'] and $_POST['jobtitle'].

<?php
$a1=array('a','b','c','d','e','f');
$a2=array('1','2','3','4','5','6');

// reset array pointers    
reset($a1); reset($a2);
while (TRUE)
{
  // get current item
  $item1=current($a1);
  $item2=current($a2);
  // break if we have reached the end of both arrays
  if ($item1===FALSE and $item2===FALSE) break;  
  print $item1.' '. $item2.PHP_EOL;
  // move to the next items
  next($a1); next($a2);
}
link|offensive?
add comment
vote up 1 vote down

I think the problem you're trying to solve, is getting a pair of values from $_POST['fullname'][] and $_POST['jobtitle'][] which have same index.

for ($i = 0, $rowcount = count($_POST['fullname']); $i < $rowcount; $i++)
{
    $name = $_POST['fullname'][$i];	// get name
    $job  = $_POST['jobtitle'][$i]; // get jobtitle
}
link|offensive?
comments (2)
vote up 0 vote down

Both Vinko and OIS' answers are excellent (I upticked OIS'). But, if you are always printing 5 copies of the text fields, you can always just name each field specifically:

<?php $i=0; while($i < 5) { ?><tr>
    ...
    <input name="fullname[<?php echo $i; ?>]" type="text" class="txt w90" id="fullname[<?php echo $i; ?>]" value="<?php echo $value; ?>" />
link|offensive?
comments (2)
vote up 3 vote down

I deleted this earlier since it was pretty close to Vinko's answer.

for ($i = 0, $t = count($_POST['fullname']); $i < $t; $i++) {
    $fullname = $_POST['fullname'][$i];
    $job_title = $_POST['job_title'][$i];
    echo "$fullname $job_title \n";
}

With original index not numerical from 0 - N-1

$range = array_keys($_POST['fullname']);
foreach ($range as $key) {
    $fullname = $_POST['fullname'][$key];
    $job_title = $_POST['job_title'][$key];
    echo "$fullname $job_title \n";
}

This is just for general info. With SPL DualIterator you can make something like:

$dualIt = new DualIterator(new ArrayIterator($_POST['fullname']), new ArrayIterator($_POST['job_title']));

while($dualIt->valid()) {
    list($fullname, $job_title) = $dualIt->current();
    echo "$fullname $job_title \n";
    $dualIt->next();
}
link|offensive?
comments (2)
vote up 0 vote down

Thank you for the replies.

If you look closely, the inputs are in a table row. I should have mentioned that this row can be duplicated on the fly; that's why I put a bracket for the inputs.

Example:

<?php $i=0; while($i < 5) { ?><tr>
  <td width="60%"><dl>
      <dt>Full Name</dt>
      <dd>
        <input name="fullname[]" type="text" class="txt w90" id="fullname[]" value="<?php echo $value; ?>" />
      </dd>
    </dl></td>
  <td width="30%"><dl>
      <dt>Job Title</dt>
      <dd>
        <input name="job_title[]" type="text" class="txt w90" id="job_title[]" value="<?php echo $value2; ?>" />
      </dd>
    </dl></td></tr><?php $i++; } ?>

The problem here is I have 2 type of array inputs (fullname & job_title).

link|offensive?
add comment
vote up 7 vote down

It's just an array:

foreach ($_POST['fullname'] as $name) {
    echo $name."\n";
}

If the problem is you want to iterate over the two arrays in parallel simply use one of them to get the indexes:

for ($i=0; $i < count($_POST['fullname']); $i++) {
    echo $_POST['fullname'][$i]."\n";
    echo $_POST['job_title'][$i]."\n";
}
link|offensive?
comments (1)

Your Answer:

Get an OpenID
or

Not the answer you're looking for? Browse other questions tagged or ask your own question.