tags:

views:

38

answers:

2

Hi all,

I would like to create a multi-dimensional array with two variables but don't know how.

This is what i have so far;

 $_SESSION['name'][] = $row_subject['name'];
 $_SESSION['history'][]= $_SERVER['REQUEST_URI'];

I wanted to know if this is possible?

$_SESSION['name'][] = $row_subject['name'],$_SERVER['REQUEST_URI'];

i want to get the name of a programme which is generated via a data base and also to retrieve the url. What i am actually doing once the name is retrieve, i want to make that a link which the url would be necessary.

any help would be appreciated.

Thanks

A: 
$_SESSION['name'][] = array($row_subject['name'], $_SERVER['REQUEST_URI']);
Coronatus
+3  A: 

I'm not sure what you want to do but the correct notation for your second example could be

$_SESSION['name'][] = array("name" => $row_subject['name'], 
                            "history" => $_SERVER['REQUEST_URI']);

This pushes an associative array with the keys "name" and "history" to the array $_SESSION["name"].

You could then access the entries like so:

echo $_SESSION["name"][0]["name"];
echo $_SESSION["name"][0]["history"];

if you repeat the command with different data:

$_SESSION['name'][] = array("name" => $row_subject['name'], 
                            "history" => $_SERVER['REQUEST_URI']);

the next entry would be addressed like so:

You could then access the entries like so:

echo $_SESSION["name"][1]["name"];
echo $_SESSION["name"][1]["history"];

and so on.

Pekka
would i be able to pull the values separately in a foreach.this is what i had. foreach($_SESSION['name'] AS $urls) { echo"<li>$urls</li><hr/>"; }would this be done?
pundit
If you follow the example above, it would have to be `foreach($_SESSION['name'] AS $urls) { echo"<li>".$urls["history"]."</li><hr/>"; }`. Whether this structure makes sense, only you can decide.
Pekka
ok. This is what is did: $_SESSION['name'][] = array("name" => $row_subject['name'], "history" => $_SERVER['REQUEST_URI']); if(empty ($_SESSION['name'])){ echo ""; } else{ $name=$_SESSION['name']; krsort($name); foreach(array_unique(array_slice($name, 0, 5)) AS $urls) { echo"<li>".$urls["name"]."</li><hr/>"; } }this however is only returning one item at a time. i would like to return like about top 5 recent items. how can i do this?
pundit
@pundit as far as I can see, if the name array contains items, the way you do it should be perfectl y fine , and not return only one item. Can you confirm how many items are in the array?
Pekka