0

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

Kleajmp
  • 167
  • 1
  • 9
  • 1
    `$_SESSION['mycart'] = "";` won't work. ` – Barmar Dec 13 '20 at 00:17
  • 1
    `var_dump($mycart); => string(52) "[18,23,24,8]"` why string(52)? because you have that js code and it gets rendered in the browser as `[18,23,24,8]` but its not JSON.. run your script again and look at the actual source i.e right-click view source or look at the response in network tools.. it's confusing as the browser is showing an array but its not on the server as an array but instead the js – Lawrence Cherone Dec 13 '20 at 00:26
  • ok that makes it all very clear now! I could not understand why I saw the array in front of me while it was not really there... It was rendered after by the browser, makes sense, thanks for the enlightenment. – Kleajmp Dec 13 '20 at 13:06

1 Answers1

-1
$string = "[18,23,24,8]";
preg_match_all('/(\d+)/', $string, $m);
var_dump($m);

This will return to you array of matches.

Array ( 
[0] => Array ( [0] => 18 [1] => 23 [2] => 24 [3] => 8 ) 
[1] => Array ( [0] => 18 [1] => 23 [2] => 24 [3] => 8 ) 
)

In you case those will be identical, you can use any $m[0], or $m[1] - doesn't matter.

cyadvert
  • 855
  • 7
  • 19