Jump to content

[SOLVED] Quicker way to make an associative array?


jibster

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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];
    }
?>

 

:)

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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.