doubledee Posted April 15, 2012 Share Posted April 15, 2012 I could use some help streamlining some Arrays that I shuffled around. Here is my code... // ************************ // Validate Form Data. * // ************************ foreach($_POST['answerArray'] as $questionID => $response){ // Transfer Data from POST to PHP array. $answerArray[$questionID] = trim($response); if (strlen($answerArray[$questionID]) < 2 || strlen($answerArray[$questionID]) > 1024){ // Invalid Answer. $errors[$questionID] = 'Answer must be 2-1024 characters.'; } }//End of VALIDATE FORM DATA // ****************************** // Attempt to Create Thoughts. * // ****************************** if (empty($errors)){ // Valid form data. // **************************** // Process each Form Field. * // **************************** foreach($answerArray as $questionID => $response){ Before I added the "Validate Form Data" section, this code... foreach($_POST['answerArray'] as $questionID => $response){ // Transfer Data from POST to PHP array. $answerArray[$questionID] = trim($response); ...was located under the "Process each Form Field" section. I am afraid that this line of code... foreach($answerArray as $questionID => $response){ ...might be working against the code above?! Isn't there anyway to say "foreach($answerArray[])" and not deal with a Key and Value per se?? Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/260961-streamlining-code/ Share on other sites More sharing options...
mofm Posted April 15, 2012 Share Posted April 15, 2012 Im not sure on what your asking help on :S ? But i will give it a go A foreach loop is used to loop through arrays and nothing else. If you do not want the key values you can just do this foreach ($array as $value) { //stuff u want to do here } Your code is slightly confusing you seem to be using $_POST['answerArray'] as an array. Im assuming you have modified the global variable somewhere else. If not then $_POST['answerArray'] will be a string and not an array. Quote Link to comment https://forums.phpfreaks.com/topic/260961-streamlining-code/#findComment-1337474 Share on other sites More sharing options...
doubledee Posted April 15, 2012 Author Share Posted April 15, 2012 Im not sure on what your asking help on :S ? But i will give it a go I am concerned that my variables will cross-pollinate because I have this block... // Validate Form Data. * // ************************ foreach($_POST['answerArray'] as $questionID => $response){ // Transfer Data from POST to PHP array. $answerArray[$questionID] = trim($response); ...followed by... // Process each Form Field. * // **************************** foreach($answerArray as $questionID => $response){ Does having this code twice create a problem or conflict... $questionID => $response) What is the scope of each of those variables in the foreach?? A foreach loop is used to loop through arrays and nothing else. If you do not want the key values you can just do this foreach ($array as $value) { //stuff u want to do here } Your code is slightly confusing you seem to be using $_POST['answerArray'] as an array. Im assuming you have modified the global variable somewhere else. If not then $_POST['answerArray'] will be a string and not an array. In my Form I have... name="answerArray[1]" So $_POST['answerArray'] is an array in an array. Debbie Quote Link to comment https://forums.phpfreaks.com/topic/260961-streamlining-code/#findComment-1337478 Share on other sites More sharing options...
mofm Posted April 15, 2012 Share Posted April 15, 2012 Cross pollinate is a confusing term :S? don't worrie about the two foreach statements cross pollinating what ever that means Just rember if you need to keep any data from the foreach statement you need to store it in a unique variable. The scope of a foreach statement is just like an if statement. The variables are not lost when the for each statement executes but the nature of the statement means that $questionID and $response will only contain data that is in the last element of the array. I hope that makes sense I think you have gotten very confused somewhere. name="answerArray[1]" <------ will not make it an array all you have done here is named it answerArray[1] so in php if you want to access its data you would need to do this $_POST['answerArray[1]'] I dont bealive you can post an array to php using html. If you can id like to know about it:) but what you can do is something like this. name=var value="1;2;3;4;5;6;7;8;9;10" <?php $array=explode(';',$_POST['var']); $echo $array[6];//7 ?> Quote Link to comment https://forums.phpfreaks.com/topic/260961-streamlining-code/#findComment-1337485 Share on other sites More sharing options...
codebyren Posted April 15, 2012 Share Posted April 15, 2012 I think you have gotten very confused somewhere. name="answerArray[1]" <------ will not make it an array all you have done here is named it answerArray[1] so in php if you want to access its data you would need to do this $_POST['answerArray[1]'] Yes it will. It'll be available as an array in $_POST['answerArray'] - PHP takes care of this. Then, doubledee, the variables assigned in a foreach statement are in the local scope so it's probably best to consider them temporary (the $key => $value part anyway) and if in doubt, you could use unset() like this: <?php foreach ($array as $key => $value) { // Do something } unset($key, $value); ?> Then you won't accidentally use unexpected values or get confused later... Quote Link to comment https://forums.phpfreaks.com/topic/260961-streamlining-code/#findComment-1337487 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.