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
https://forums.phpfreaks.com/topic/48258-foreach-and-2d-arrays/
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
https://forums.phpfreaks.com/topic/48258-foreach-and-2d-arrays/#findComment-235891
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
https://forums.phpfreaks.com/topic/48258-foreach-and-2d-arrays/#findComment-235949
Share on other sites

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.