如何从php中的$ _SESSION数组中完全删除空键?

I start the session by following command:

session_start();

when the user logs in I save information of that user in the $_SESSION with following code:

$_SESSION["logged_in_user"]=$username;  
$_SESSION["logged_in_status"]="logged in";
$_SESSION["logged_in_business_id"]=$business_id;
$_SESSION["logged_in_business"]=$business_name;

and when the user logs out I do the following commands to clear the $_SESSION :

setcookie("PHPSESSID", "", time()-3600, "/" );
$_SESSION=array();
session_destroy();
session_write_close();

Now when I check the $_SESSION after logout and before login, the result is an empty array(), which is exactly what it should be. But as soon as I log in $_SESSION not only shows the information I saved in it, but also empty values with filled keys, like for example:

Array(
[logged_in_user] => aristocraticmasterminder
[logged_in_status] => logged in
[logged_in_business_id] => us-000000001
[logged_in_business] => Microsoft PLC Limited
[RoleID] => 
[RoleName] => 
[RoleDuty1] => 
);

These keys do not have any value saved in $_SESSION corresponding to them. I session_start(); on the top of each page, and consequently also on the log out page before session_destroy(); I think this has something to do with the PHPSESSID cookie which is saved on my browser. How can I clear these empty keys and just save the data saved after the session_start();?

try this

    unset($_SESSION['name']);

How to remove a variable from a PHP session array

Reverse your session destruction sequence.

Change:

$_SESSION=array();
session_destroy();

To:

session_destroy();
$_SESSION=array();