Passing JSON Object as Function Argument

I have a strinigified Object which I am passing to a PHP file via the fetch API.

Looks like this: [{\"key\":\"value\"}]

I can access and save this data directly to the database by simply storing it into a variable in my PHP file: $myObject = $_POST['myobject'];

However, when I try to pass $myObject as a function argument, the next function returns nothing.

I tried JSON encode, JSON Decode, stripslashes, etc but to no avail. Does anyone understand what this is about?

When I pass a simple variable to my PHP function such as an integer and grab it with $myInteger = $_POST['myinteger']; I can pass this to another function and it grabs the value just by referencing the argument name. So it is working for this but not for the myObject. FYI variable names are not literal.

I would guess this is down to the particular function. Can you show the function?

When I manually create the string '[{\"key\":\"value\"}]' with single quotes, I can pass this as an argument. But when I try to create [{\"key\":\"value\"}] a string from the data stored in the variable it won’t pass the data as an argument. I’m assuming the argument is expecting a string only but am not able to figure out how to convert it. Try strval, concatenating “'” . $var . “'”; but nothing.

The second function is getting the data actually. I’m just not extracting the values correctly. Used to using JSON.parse(variable) via JavaScript. Need to parse the the values from an associative array in PHP now.

Probably just need a foreach loop.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.