help, I'm sorry to make it so complex but this drives me crazy:
I have an array of numbers passed from js to a php $_SESSION['mycart']
variable.
If the customer selected four products on page1, an array of four ids should be passed (trough a php session variable) to page2 as a string looking like this: "[18,23,24,8]"
.
(I could not use $.post to pass the data, as a user can visit other pages in between and then the formdata would be lost. To pass the data from js to php I used a strange trick like: $_SESSION['mycart'] = "<script>document.write(sessionStorage.cart)</script>";
Maybe this is not the best way but it works.)
Here is the actual problem: when I'm on page2 I try to read out the session variable and I would like to convert it to a php array which I can use later to get info from mysql.
//import mycart
$mycart = $_SESSION['mycart'];
$decode = json_decode($mycart);
$nobrackets = str_replace("[","",$mycart); $nobrackets = str_replace("]","",$nobrackets);
$array = explode(",", $nobrackets);
So: This string needs to be converted into a php array containing the ids of the selected products. (I tried literally more than 3 hours to accomplish this, Googled for hours, I feel lost and stupid now. How someone can help here.)
this is the output I get from my debug:
echo $_SESSION['mycart']; => [18,23,24,8] //ok those 4 numers I need...
var_dump($mycart); => string(52) "[18,23,24,8]" //why string(52)? I see only 12 characters in my string?
var_dump($mycart[0]); => string(1) "<" //so the first character of the string is "<" => huh, it should be "[" no?
var_dump($decode); => NULL //so this is not a string that can be decoded by json_decode(), so I skip this option.
var_dump($nobrackets); => string(52) "[18,23,24,8]" //so replacing both brackets by str_replace() does not work, why not?
var_dump($array); => array(1) { [0]=> string(52) "[18,23,24,8]" } //the plan was to first remove the brackets and after explode the string into a new php array by using the explode function with an "," delimiter. But the output is again the array I started with.
This drives me crazy. What am I overseeing? How can I get this "[18,23,24,8]" string to an actual array? Please ask if I need to put more info instead of -1