Forum Moderators: coopster
I have a form that users fill in data into text boxes and submit. An array is created from the text boxes(the print_r is below):
Array (
[TextBox1] => Array ( [name] => TextBox1 [value] => ...someText... [show] => 1 [required] => 1 [text] => ...enteredText1...)
[TextBox2] => Array ( [name] => TextBox2 [value] => ...someMoreText... [show] => 1 [required] => 1 [text] => ...enteredText2...)
[TextBox3] => Array ( [name] => TextBox3 [value] => ...evenMoreText... [show] => 1 [required] => 1 [text] => ...enteredText3...)
[TextBox4] => Array ( [name] => TextBox4 [value] => ...LastBitOfText... [show] => 1 [required] => 1 [text] => ...enteredText4...)
)
The loop that processes this array is:
foreach($TextBoxData as $text)
{
if($text['text']!= "")
{
$msg .= "- In Response to: \n";
$msg .= " ".filter_text($text['value'], "nohtml")." \n";
$msg .= " - They said: \n";
$msg .= " ".filter_text($text['text'], "nohtml")." \n";
$msg .= "\n";
}
}
the output i get is:
-In Resonse to:
...someText...
-They said:
...enteredText1...
-In Resonse to:
...someMoreText...
-They said:
...enteredText2...
-In Resonse to:
...evenMoreText...
-They said:
...enteredText3...
-In Resonse to:
...evenMoreText...
-They said:
...enteredText3...
It repeats the second to last, and omits the last completely.
if i change the loop to:
foreach($TextBoxData as &$text)
This has been reported as a bug [bugs.php.net] and found to be expected behavior. Refer to that page for more information. I have to say that I do not like it this behavior; never ran into it, though.
good luck
However, unsetting the value immediately after each foreach() exited corrected the problem.
[edited by: natbur at 9:20 pm (utc) on June 10, 2007]
[edit reason]changed from minutes to seconds ;)[/edit]
[edited by: eelixduppy at 9:23 pm (utc) on June 10, 2007]
And just out of curiosity, does the following code produce the same results, because it is working fine on my machine:
$array = array(
'TextBox1' => array('value' => 'someText1','text' => '...enteredText1...'),
'TextBox2' => array('value' => 'someText2','text' => '...enteredText2...'),
'TextBox3' => array('value' => 'someText3','text' => '...enteredText3...'),
'TextBox4' => array('value' => 'someText4','text' => '...enteredText4...')
);
#
$msg = '';
foreach($array as $text)
{
if($text['text']!= "")
{
$msg .= "- In Response to: \n";
$msg .= " ".$text['value']." \n<br>";
$msg .= " - They said: \n";
$msg .= " ".$text['text']." \n<br>";
$msg .= "<br>\n";
}
}
echo $msg;
foreach($TextBoxData as &$text)
{
if($text['required'] == 1 && $text['text'] == "")
{
$text['missing'] = 1;
$missing++;
}
}
unset($text);
Adding that unset fixed the problem(It's always fun finding problems a couple hundred lines further up...). It's strange that PHP scope doesn't destroy the $text variable as soon as the foreach exits.
I thought perhaps it was an internal pointer issue, so I tried resetting the array just prior to the loop, but that doesn't fix it. For now, I'll just make a habbit of unsetting the temp variable after each foreach loop that I call with a reference.