February 25, 200917 yr Hi all, I've got a session array added to on each page using the following: $_SESSION['recently-viewed'][] = array('sport'=> $sport,'event'=>$currentevent,'market'=>$currentmarket); I can get it to display (with no styling) using print_r($_SESSION['recently-viewed']) but what I need is to display and store each value in the array separately when retrieving. I've tried the following two attempts to no avail. Any ideas? foreach($_SESSION['recently-viewed'] as $key => $val) { echo $val." = ".$key."<br />"; } foreach($_SESSION['recently-viewed'][] as $key => $val) { echo $val." = ".$key."<br />"; }
February 25, 200917 yr If I hard code the variables, the following works ok. <?php $sport = 'tennis'; $currentevent = 'cup'; $currentmarket = 'bids'; $_SESSION['recently-viewed'] = array('sport'=> $sport,'event'=>$currentevent,'market'=>$currentmarket); foreach($_SESSION['recently-viewed'] as $key => $val) { echo $val." = ".$key."<br />"; } ?> Where are you getting the $sport, $currentevent and $currentmarket variables from?
February 25, 200917 yr Author My $sport, $currentevent and $currentmarket variables echo as should do before the statement. It may be a problem with how I am storing it in the session i.e. $_SESSION['recently-viewed'][] = array('sport'=> $sport,'event'=>$currentevent,'market'=>$currentmarket); I'm trying to store an array of sport, event, market inside of another array called recently-viewed in the session.
February 25, 200917 yr Author But doing it how you've coded it means I can only store one array in 'recently-viewed'. I need to store multiple arrays inside that array.
February 25, 200917 yr Sorry, not a mind reader You need to use something like this: <?php $sport = 'tennis'; $currentevent = 'cup'; $currentmarket = 'bids'; $_SESSION['recently-viewed'] = array( array ('sport'=> $sport,'event'=>$currentevent,'market'=>$currentmarket), array('sport'=> $sport2,'event'=>$currentevent2,'market'=>$currentmarket2), array('sport'=> $sport3,'event'=>$currentevent3,'market'=>$currentmarket3) ); ?> Have a look at something like this, it might help you out
February 26, 200917 yr Author Sorry, not a mind reader Not a reader either . I did say it was an array within array in my 2nd post. http://www.webcheatsheet.com/php/multidimensional_arrays.php Yeah that helped thanks. It seems to be a foreach within foreach. It was just the syntax I couldn't get my head around. The working code is below for anyone who struggles with the same problem. -- STORING MULTIDIMENSIONAL ARRAYS IN SESSIONS -- // Each page adds its details to the array $_SESSION['p'][] = array('sport'=> $sport,'event'=>$currentevent,'market'=>$currentmarket); // Set up array within array $_SESSION['recently-viewed'] = array('p'); foreach($_SESSION['recently-viewed'] as $key => $value) { //Transfer the name of each set into $rv $rv = $_SESSION['recently-viewed'][$key]; //Loop through each field array foreach($_SESSION[$rv] as $field => $value) { $sport = $_SESSION[$rv][$field]['sport']; echo $sport; $event = $_SESSION[$rv][$field]['event']; echo $event; $market = $_SESSION[$rv][$field]['market']; echo $market; } }
Create an account or sign in to comment