Jump to content

foreach and 2D arrays


Crusader

Recommended Posts

This is my 2D array:

$sectors = array ( 'x' => $map['x'],
	   'y' => $map['y'],
	   'link'=> $game->makeLink(
	);

 

Will the following foreach call generate sessions for the above array? I don't have any way to test it right now :(

/**
* Generate sessions for coordinate arrays.
*/
function mapLinks()
{
foreach($this->sectors as $k => $v) {
	$sess = $this->sectors[$k]['link'];
	$_SESSION[$sess] = array ( "x" => $this->sectors[$k]['x'], 
				   "y" => $this->sectors[$k]['y']
				);
}
}

 

Thanks.

Link to comment
Share on other sites

Thanks but I know how to output arrays. I actually want to store the arrays in a session.

 

note: $map is an array as well. it's a mysql_fetch_array

 

It should output like this:

 

$_session(14u01jr0af0m2) = array (x => 1, y =>3);

$_session(10rm0WFG02af) = array (x => 3, y =>5);

Link to comment
Share on other sites

The easiest way to store the array in a session, is to store the whole array in the sessions:

<?php
session_start();
$sectors = array ( 'x' => $map['x'],
	   'y' => $map['y'],
	   'link'=> $game->makeLink(
	);
$_SESSION['sectors'] = $sectors;
?>

The you retrieve it with:

<?php
session_start();
$sectors = $_SESSION['sectors'];
?>

 

Ken

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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