user2772219
user2772219

Reputation: 129

How to convert JSON into an array or string?

I'm not sure if I'm allowed to ask these types of questions. Anyway, I have this array value $custom_fields['user_gender'][0] that outputs this:

a:2:{i:0;s:7:"Male";i:1;s:7:"Female";}

I'm not sure how to convert this into an array or string.

Upvotes: 1

Views: 135

Answers (1)

edwardmp
edwardmp

Reputation: 6601

The example data looks like it is serialized PHP. Serializing is a way an array, string etc. can be "represented" as a textual string and can later be converted back to the original data format, using a unserialize operation. A serialized string can be stored as text in a database and can be unserialized at any point.

You can unserialize it with:

$array = unserialize($custom_fields['user_gender'][0]);

This should return an array. If you var_dump on $array, you can be sure it's an array.

Upvotes: 3

Related Questions