Lamez Posted December 20, 2009 Share Posted December 20, 2009 Okay so what I am trying to do is call a certain value out of an array, which is stored in a $_SESSION variable (which is also an array). The array in question is my errorArray, and in the registration process, if an error occurs, I add the error to the errorArray. That part works just fine, it is just getting the value out. Here is what I have so far: <?php function getError($field){ return $_SESSION['errorArray'][$field]; } //example echo getError("email"); ?> This returns nothing. Any ideas? -Thanks! (again.) Link to comment https://forums.phpfreaks.com/topic/185737-calling-values-out-of-two-arrays/ Share on other sites More sharing options...
printf Posted December 20, 2009 Share Posted December 20, 2009 I don't see any session_start(); in your script. Evey page must have session_start() before trying to use the SUPER GLOBAL $_SESSION! <?php session_start (); function getError($field){ return $_SESSION['errorArray'][$field]; } //example echo getError("email"); ?> p! Link to comment https://forums.phpfreaks.com/topic/185737-calling-values-out-of-two-arrays/#findComment-980733 Share on other sites More sharing options...
Lamez Posted December 20, 2009 Author Share Posted December 20, 2009 that is a snippet of the code. You don't want the whole code. Link to comment https://forums.phpfreaks.com/topic/185737-calling-values-out-of-two-arrays/#findComment-980735 Share on other sites More sharing options...
Catfish Posted December 20, 2009 Share Posted December 20, 2009 if it's returning nothing, then $_SESSION['errorArray']['email'] is empty. Checked your spelling/case sensitivity? Link to comment https://forums.phpfreaks.com/topic/185737-calling-values-out-of-two-arrays/#findComment-980739 Share on other sites More sharing options...
Lamez Posted December 20, 2009 Author Share Posted December 20, 2009 Question, does this look right? array(6) { [0]=> array(1) { ["email"]=> string(21) "Email field is empty." } [1]=> array(1) { ["email2"]=> string(21) "Email field is empty." } [2]=> array(1) { ["pass"]=> string(24) "Password field is empty." } [3]=> array(1) { ["pass2"]=> string(24) "Password field is empty." } [4]=> array(1) { ["first"]=> string(20) "First name is empty." } [5]=> array(1) { ["last"]=> string(19) "Last name is empty." } } I did this to get that information: <?php var_dump($_SESSION['errorArray']); ?> Here is the function that adds the values to the array <?php function addError($field, $error){ if(!array_key_exists($field, $error)){ $_SESSION['errorArray'][] = array($field => $error); } } ?> Does all that look right to you guys? Link to comment https://forums.phpfreaks.com/topic/185737-calling-values-out-of-two-arrays/#findComment-980740 Share on other sites More sharing options...
Buddski Posted December 20, 2009 Share Posted December 20, 2009 Your error starting function. function addError($field, $error){ if(!array_key_exists($field, $error)){ $_SESSION['errorArray'][] = array($field => $error); } } is assigning the errors to the next available space in the errorArray. For your retrieval function to work as you expect your starting code should be something like this.. function addError($field, $error){ $_SESSION['errorArray'][$field] = $error; } Link to comment https://forums.phpfreaks.com/topic/185737-calling-values-out-of-two-arrays/#findComment-980747 Share on other sites More sharing options...
Lamez Posted December 20, 2009 Author Share Posted December 20, 2009 Thank you so much, that fixed everything! I am new to arrays, and the manual did not solve my problems. Thanks again! Link to comment https://forums.phpfreaks.com/topic/185737-calling-values-out-of-two-arrays/#findComment-980748 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.