niche Posted March 13, 2012 Share Posted March 13, 2012 I thought this code would allow me to accumulate separate $_POST inputs, but it won't cycle on the first condition. Any ideas? Is there a better way to test for whether an array is set? code: if (isset($curacc2)) { echo "here1"; $curacc2 = array_merge($curacc2,$_POST);} else { echo "here2"; $curacc2 = $_POST;}echo var_dump($curacc2) . '<br/>'; Link to comment Share on other sites More sharing options...
justsomeguy Posted March 13, 2012 Share Posted March 13, 2012 You can use is_array to check if a variable is an array, but if it's not set then it's also not going to be an array. Link to comment Share on other sites More sharing options...
niche Posted March 13, 2012 Author Share Posted March 13, 2012 I could just merge $_POST to $_SESSION and accumulate them there, but I'd like to send them to an array dedicated to only accumulating inputs to $_POST. Is there another "$_SESSION - type" array that's available in php? EDIT: What about the $_COOKIE array? Link to comment Share on other sites More sharing options...
niche Posted March 13, 2012 Author Share Posted March 13, 2012 Maybe the answer is to create a multidimensional in $_SESSION. If that's possible, how would I add $_POST as a different dimension in $_SESSION? I've never created a new dimension from an array. Link to comment Share on other sites More sharing options...
astralaaron Posted March 13, 2012 Share Posted March 13, 2012 couldn't you just store an array within the session array? Link to comment Share on other sites More sharing options...
niche Posted March 13, 2012 Author Share Posted March 13, 2012 That's what I was thinking. Please see post #4. How do I do that? Link to comment Share on other sites More sharing options...
justsomeguy Posted March 13, 2012 Share Posted March 13, 2012 $_SESSION['post'] = $_POST; Link to comment Share on other sites More sharing options...
niche Posted March 13, 2012 Author Share Posted March 13, 2012 Thanks. EDIT: $_SESSION['post'] is the first position. What's the notation for the second? Link to comment Share on other sites More sharing options...
justsomeguy Posted March 13, 2012 Share Posted March 13, 2012 I don't know what you mean by "accumulate". That code copies everything that is in $_POST into $_SESSION['post'] and overwrites what is already there. If $_POST is empty, then $_SESSION['post'] will be empty. If that's not what you're trying to do, explain exactly what you're going for. If you want to have statements on multiple pages that all process $_POST, and you want to collect all of those values from all submissions into one array, then you do use array_merge for that. if (!isset($_SESSION['post'])) $_SESSION['post'] = array();$_SESSION['post'] = array_merge($_SESSION['post'], $_POST); Link to comment Share on other sites More sharing options...
niche Posted March 13, 2012 Author Share Posted March 13, 2012 Thanks justsomeguy. Works like a charm. Though I've forgotten what the difference is between !isset() and isset()? Link to comment Share on other sites More sharing options...
justsomeguy Posted March 13, 2012 Share Posted March 13, 2012 The ! operator is "not". Link to comment Share on other sites More sharing options...
niche Posted March 13, 2012 Author Share Posted March 13, 2012 Thanks. What's the offset for the second element in the $_SESSION['post'] dimension? I thought this would work, but it didn't: if (isset($_SESSION['post'][2])) { echo $_SESSION['post'][2]; echo "HERE"; } Link to comment Share on other sites More sharing options...
justsomeguy Posted March 13, 2012 Share Posted March 13, 2012 The second offset would be 1, but that assumes that $_POST is an indexed array instead of associative. Use print_r on it if you want to check what's in it. Link to comment Share on other sites More sharing options...
niche Posted March 13, 2012 Author Share Posted March 13, 2012 I suppose it's not. I thought you could access an indexed array through offsets. Is there a function to convert indexed to numeric or access an indexed with offsets? Link to comment Share on other sites More sharing options...
justsomeguy Posted March 13, 2012 Share Posted March 13, 2012 Indexed is numeric, associative is when the names are strings. You can use a foreach loop if you want to loop through the array and add the items to an indexed array. You can also use the array_values function to get just the values and lose the keys. Link to comment Share on other sites More sharing options...
niche Posted March 14, 2012 Author Share Posted March 14, 2012 Thank-you very much for your help justsomeguy and astraaron. array_values() did the final trick. I can begin to put multidimensional arrays under my belt. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.