aibarra aibarra - 2 months ago 38
PHP Question

WP REST API - Strange formating in response

So I'm working with the wp rest api and the wp rest controller plugin. I have a music theme that's doing some interesting things. When i make the API call one of the properties i get is this:

"subheader_img": [
"a:1:{i:0;a:5:{s:5:\"image\";s:0:\"\";s:5:\"color\";s:0:\"\";s:6:\"repeat\";s:6:\"repeat\";s:8:\"position\";s:8:\"left top\";s:11:\"attachement\";s:6:\"scroll\";}}"

its a string, that i know should be an array. I'm not sure what type encoding is applied to it. But i'm trying to clean it up on the javascript side. Does anyone know what's being done to this array turned string? And if there are any javascript functions that can help? I've tried JSON.Parse and that obviously won't work because... its not JSON. I've also tried stripping away the first few characters string.substring(x), but i run into the same problem after that.

i know its something simple, i'm just not sure what.

If its possible I'd like to parse it in javascript, modifying the endpoint might be more difficult because of the wordpress theme.



Use WP's functions for serialization:

The gist: Check if your array item is a serialized string (is_serialized_string), and then maybe_unserialize it. Then you can evaluate the result to see if it's in a format you're expecting (such as an array).