tlavelle Posted August 20, 2007 Share Posted August 20, 2007 Could someone please explain the code snippet below line by line? I especially am interested in the last line. Is that a multidimensional array? How does the foreach() iterate across it? Thanks $_SESSION['Posted_values'] = array(); foreach($_POST as $fld => $val) $_SESSION['Posted_values'][$fld] = $val; Quote Link to comment https://forums.phpfreaks.com/topic/65824-please-explain-this-code-snippet/ Share on other sites More sharing options...
MadTechie Posted August 20, 2007 Share Posted August 20, 2007 its the same as $_SESSION['Posted_values'] = $_POST; copies the post to a session Quote Link to comment https://forums.phpfreaks.com/topic/65824-please-explain-this-code-snippet/#findComment-328889 Share on other sites More sharing options...
tlavelle Posted August 20, 2007 Author Share Posted August 20, 2007 Thanks again. Please be a little more specific in your response. I am pretty new and pretty thick. Ambiguous pronouns such as "it" aren't specific enough. Quote Link to comment https://forums.phpfreaks.com/topic/65824-please-explain-this-code-snippet/#findComment-328897 Share on other sites More sharing options...
Wuhtzu Posted August 20, 2007 Share Posted August 20, 2007 It pretty straight forward: $_SESSION['Posted_values'] = array(); This line just create a session variable called 'Posted_values' and assign an empty array to it. foreach($_POST as $fld => $val) This line starts a foreach() loop which takes each element in the $_POST array and each cycle in the loop it assigns the $_POST arrays key to the variable $fld and its value to $val. For example will $_POST['name'] = "Wuhtzu" become $fld = "name" and $val = "Wuhtzu". $_SESSION['Posted_values'][$fld] = $val; Here the $_SESSION['Posted_values']-array is populated with values... The above example will result in $_SESSION['Posted_values']['name'] = "Wuhtzu" And yes it's a 2d-array. MadTechie meant that the code you posted can be replaced with $_SESSION['Posted_values'] = $_POST; Quote Link to comment https://forums.phpfreaks.com/topic/65824-please-explain-this-code-snippet/#findComment-328898 Share on other sites More sharing options...
MadTechie Posted August 20, 2007 Share Posted August 20, 2007 $_SESSION['Posted_values'] = array(); foreach($_POST as $fld => $val) $_SESSION['Posted_values'][$fld] = $val; is the same as doing this $_SESSION['Posted_values'] = $_POST; which copies the $_POST array into the session 'Posted_values' not sure how else to explain it!! EDIT: nice one Wuhtzu Quote Link to comment https://forums.phpfreaks.com/topic/65824-please-explain-this-code-snippet/#findComment-328901 Share on other sites More sharing options...
tlavelle Posted August 20, 2007 Author Share Posted August 20, 2007 Thank you very very much for that explanation. That totally elucidated the snippet for me. Thanks for the lesson. Quote Link to comment https://forums.phpfreaks.com/topic/65824-please-explain-this-code-snippet/#findComment-328903 Share on other sites More sharing options...
Wuhtzu Posted August 20, 2007 Share Posted August 20, 2007 MadTechie: It is what happens when you haven't got any projects to work on Quote Link to comment https://forums.phpfreaks.com/topic/65824-please-explain-this-code-snippet/#findComment-328925 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.