0

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 value Commented 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? Commented Jan 28, 2015 at 13:29

1 Answer 1

2

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the hyper-fast answer. Does exactly what I need. I will accept it asap.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.