Jump to content

Calling values out of two arrays.


Lamez

Recommended Posts

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

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!

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?

 

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;
} 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.