Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Sessions and Arrays - Small query

Featured Replies

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 />";

}

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?

  • 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.

  • 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.

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 :)

  • Author
Sorry, not a mind reader

 

Not a reader either ;) . I did say it was an array within array in my 2nd post.

 

 

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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.