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?
2
-
first get exist value then replace with exist+new valueRakesh Sharma– Rakesh Sharma2015-01-28 13:21:00 +00:00Commented Jan 28, 2015 at 13:21
-
Why do I get a negative vote for this question? Downvoter thinks its better for the community to just see the correct answer and delete question?Leonidas– Leonidas2015-01-28 13:29:26 +00:00Commented Jan 28, 2015 at 13:29
Add a comment
|
1 Answer
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;
}
1 Comment
Leonidas
Thanks for the hyper-fast answer. Does exactly what I need. I will accept it asap.