684425 Posted June 15, 2020 Share Posted June 15, 2020 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 Quote Link to comment Share on other sites More sharing options...
gw1500se Posted June 15, 2020 Share Posted June 15, 2020 Just use += (for PHP 5.4+). $_SESSION['users']+=["boss"=>$row["bossid"]]; For 5.3- $_SESSION['users']+=array("boss"=>$row["bossid"]); Quote Link to comment Share on other sites More sharing options...
requinix Posted June 15, 2020 Share Posted June 15, 2020 Keep in mind that the + operator (and +=) for arrays may not work the way you think it does. Quote Link to comment Share on other sites More sharing options...
Daniel290499 Posted June 17, 2020 Share Posted June 17, 2020 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 Quote Link to comment Share on other sites More sharing options...
requinix Posted June 17, 2020 Share Posted June 17, 2020 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. Quote Link to comment Share on other sites More sharing options...
Daniel290499 Posted June 17, 2020 Share Posted June 17, 2020 O yes. Right you are. Good catch. Points for being close...? Quote Link to comment Share on other sites More sharing options...
requinix Posted June 17, 2020 Share Posted June 17, 2020 1 minute ago, Daniel290499 said: Points for being close...? 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. Quote Link to comment Share on other sites More sharing options...
Daniel290499 Posted June 17, 2020 Share Posted June 17, 2020 Indeed, indeed. Well said. Quote Link to comment Share on other sites More sharing options...
684425 Posted July 23, 2020 Author Share Posted July 23, 2020 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🙂 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.