Reputation: 566
I have a variable which changes its value in every ajax request. What I want to accomplish is to have a session array $_SESSION["tmp_arr"] and fill it with the values of this same variable. I dont want to overwrite the value of the $_SESSION variable, but append it in a array. Which is the correct way to accomplish that?
Upvotes: 0
Views: 63
Reputation: 96159
If there is no such element in _SESSION or if it's not an array create a new one with the first/initial value. Otherwise append the new value to the existing array.
session_start();
[...]
if ( !isset($_SESSION["tmp_arr"]) || !is_array($_SESSION["tmp_arr"]) ) {
$_SESSION["tmp_arr"] = array( $newValue );
}
else {
$_SESSION["tmp_arr"][] = $newValue;
}
Upvotes: 2