jibster Posted April 1, 2008 Share Posted April 1, 2008 Hi there. This forum has been valuable in the past so I'm going to give you another if that's ok Is there a quicker way of creating an associative array than this? $my_array = array('Name'=>$_SESSION['Name'], 'Age'=>$_SESSION['Age'], 'Talent'=>$_SESSION['Talent'], 'Interests'=>$_SESSION['Interests'], 'Ambitions'=>$_SESSION['Ambitions']); That may seem like simple code but the array above is just an example I made up. My actual one is more like 25 items with much longer names. As you can see the Key name is the same as the Value name inside the session which is why I thought there may be an alternative. Cheers guys Jon Quote Link to comment Share on other sites More sharing options...
discomatt Posted April 1, 2008 Share Posted April 1, 2008 No, not really. You could always take every entry of the $_SESSION array and dump it into another (Why, i don't know), but if you only want specific variables moved to the other array... no. Quote Link to comment Share on other sites More sharing options...
keeB Posted April 1, 2008 Share Posted April 1, 2008 $my_arr = array(); foreach($_SESSION as $k=>$v) { $my_arr[$k] = $v; } Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 1, 2008 Share Posted April 1, 2008 You could create an array of the keys you want to move and then use a foreach loop: <?php $keys = array('Name','Age','Talent','Interests','Ambitions'); $my_array = array(); foreach ($keys as $k) $my_array[$k] = $_SESSION[$k]; ?> But, why do you want to use another array when the $_SESSION array can be used? Ken Quote Link to comment Share on other sites More sharing options...
coder_ Posted April 1, 2008 Share Posted April 1, 2008 and to avoid any errors: <?php $keys = array('Name','Age','Talent','Interests','Ambitions'); $my_array = array(); foreach ($keys as $k) if (isset($_SESSION[$k])) { $my_array[$k] = $_SESSION[$k]; } ?> Quote Link to comment Share on other sites More sharing options...
discomatt Posted April 1, 2008 Share Posted April 1, 2008 Those are odd ways of doing it though. You get the job done, but have to call a loop, define a third array and make a function call. The most efficient way is to define them manually. $my_arr = array(); foreach($_SESSION as $k=>$v) { $my_arr[$k] = $v; } Or you could just go $my_arr = $_SESSION; Quote Link to comment Share on other sites More sharing options...
keeB Posted April 1, 2008 Share Posted April 1, 2008 If we're talking efficiency then why are we telling him how to do this? lol. Parsing out to put it somewhere else.. sounds logical to me (not!) Quote Link to comment Share on other sites More sharing options...
discomatt Posted April 1, 2008 Share Posted April 1, 2008 I could see it being logical to a point... say you have 50 session vars, but there are 10 you need to use in loops through the script. Putting them in their own array would save you from quite a bit of code. Then again, the ideal thing to do in that case would be to have those 10 vars in their own array within the $_SESSION array. Quote Link to comment Share on other sites More sharing options...
jibster Posted April 1, 2008 Author Share Posted April 1, 2008 Cheers for the replys. To explain, I was creating a shopping basket last nite. My goal is an array called $basket with another array holding details of a single certificate that has been ordered. User may add many certificates. So that's a two dimensional array so far but then the basket itself is held in the session array. So now it's 3 dimensional. To make things more confusing the details that need to be added to the basket are stored in the session as single vars. Anyway, I can get to this point: $_SESSION['basket'][3][1] $_SESSION['basket'][3][2] etc. The 1 and 2 being the 1st and 2nd element of the 3rd certificate in the basket respectively. What i wanted was: $_SESSION['basket'][3]['firstname'] $_SESSION['basket'][3]['lastname'] but as I'm using array_push(), an associative array is not possible. Maybe I'm going about this completely the wrong way. I suppose I could just get by with $_SESSION['basket'][3][1], knowing that the 1 was the firstname 2 was the lastname etc., but an associative array would just be nicer. I'm not sure I've made complete sense there have I? :-\ Lemme know. Thanks for any help here, Jib Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 1, 2008 Share Posted April 1, 2008 If you had explained what you wanted to do in the first place, perhaps you would have gotten a different set of answers. Ken Quote Link to comment Share on other sites More sharing options...
jibster Posted April 1, 2008 Author Share Posted April 1, 2008 Yeah your probably right, I was just trying to make things simple. Some posters here post their full source for a page when only a couple of lines are required. Apologies. Cheers Jib Quote Link to comment Share on other sites More sharing options...
keeB Posted April 2, 2008 Share Posted April 2, 2008 <?php $items = array("name" => "keeb"); foreach ($items as $ikey => $item) { $arr =$_SESSION['basket']["items"][$ikey] = $item // ? } ?> Quote Link to comment Share on other sites More sharing options...
jibster Posted April 2, 2008 Author Share Posted April 2, 2008 Thanks keeb, works gret. Jib Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.