Hi everyone, I have a json string that is input by the user on an form in the edit post tab. I’m having trouble parsing it correctly after getting the string from the database:
$rawdata = get_post_meta( $current_id, ‘lele’, true );
$newdata = json_decode( $rawdata );
foreach ($newdata as $mydata) {…
Warning: foreach() argument must be of type array|object, string given in /var/www/atuff on line 29
I tried both to use stripslashes before and after decoding but with no use, is wordpress butchering my JSON strings? How can I retrieve data without any external tampering?
Many thanks!
[ad_2]
can you post an example of the json by any chance?
If you var_dump( get_post_meta( $current_id, ‘lele’, true ) ); what does the value look like? Is it even a json string?
No it’s not butchering your json. It’s expecting an array, and not finding it due to the json itself or something in your function. Post the json and the full function.
first check if $rawdata contains what you expect it to.
then check if $newdata contains what you think it should
Look at the actual data in the database. If it’s not json, then json decode won’t work.
Also, you do not have to store data encoded as json. WordPress can also store PHP arrays just fine. Built-in.