Jump to content

Adding more elements to a session array


684425

Recommended Posts

I have created a session array on one page and stored some values as

$_SESSION['users'] = array(
  "id"      => $row['id'],
  "fname"   => $row['ufname'],
  "lname"   => $row['ulname']
);

and then on another page I have some more values for the same session array

  "boss"      => $row['bossid'],
  "tasks"     => $row['tasks'],
  "timeframe" => $row['tframe']

I want to add these values into session array both keys and values.

is there any way of doing this? Please help

Link to comment
Share on other sites

You can use array_push() to add elements to an array.

$newArray = array();
$newArray['boss'] => $row['bossid'];
$newArray['tasks'] => $row['tasks'];
$newArray['timeframe'] => $row['tframe'];

array_push($_SESSION['users'] , $newArray);

Instead of creating $newArray, you can just insert $row into the array.

I did it this way because I don't know what else is in $row.

It's a personal choice, but I break up my code a lot.

I hope this helps :)

Link to comment
Share on other sites

1 hour ago, Daniel290499 said:

You can use array_push() to add elements to an array.

That will add the $newArray array into the users array under a "0" key. As in $_SESSION[users] will be an array with an "id", "fname", "lname", and "0".

The answer is array_merge/replace or careful use of the +/+= operator.

Link to comment
Share on other sites

1 minute ago, Daniel290499 said:

Points for being close...? :D

Part of the way there. array_push could be useful except for one rather important limitation: you can't choose the array keys it uses. And since the keys are important here, array_push won't cut it.

Also note that array_push is basically just a function version of the [] operator. As in array_push($array, $value) is the same as $array[] = $value. It does allow pushing multiple values at once, though, and sometimes that is useful.

Link to comment
Share on other sites

  • 1 month later...
On 6/17/2020 at 2:05 PM, requinix said:

That will add the $newArray array into the users array under a "0" key. As in $_SESSION[users] will be an array with an "id", "fname", "lname", and "0".

The answer is array_merge/replace or careful use of the +/+= operator.

Sir if i choose array_merge then how can i add new items to the existing session array?

i mean do i need to consider the existing session array as temporary session array and also i have to store new session values to a new temporary session array and then merge those two temporary session arrays into one new session array?

Existing session array created on one page and the session is set

$_SESSION['users'] = array(
  "id"      => $row['id'],
  "fname"   => $row['ufname'],
  "lname"   => $row['ulname']
);

New session values on another page that i need to add to the existing session array

  "boss"      => $row['bossid'],
  "tasks"     => $row['tasks'],
  "timeframe" => $row['tframe']

I was thinking to choose array_push() for this but your reply confused me sir🙂

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.