1

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.

3
  • I assume you use PHP? Commented Nov 17, 2014 at 1:15
  • You might be allowed if you make your question a little bit more clear. Such as on which language do you want to use it? I am guessing php, am I right? Commented Nov 17, 2014 at 1:16
  • Sorry guys. Yes, it's PHP Commented Nov 17, 2014 at 1:19

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Will accept this answer in a few mins. Anyway, it outputs this array(2) { [0]=> string(5) "Male" [1]=> string(6) "Female" } how do I let it show only Male and Female?
Yes it is an array. You can use $array[0] for male and $array[1] for female.
I have a follow up question. I did this: $user_gender = unserialize($custom_fields['user_gender'][0]); And then I did this $user_gender = array_map('strtolower', $user_gender); But I get this error 'Warning: array_map(): Argument #2 should be an array'
As the error tells, $user_gender is not an array, it is a string. If you just want to lowercase the string just use strtolower($user_gender) instead. The array_map would apply that function to every string in array, but using it on a string is not going to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.