0

I have a Wordpress post meta to MySql like

[{"field":"Email:1","title":"email","explanation_text":"","explanation_text_location":"","html_styling":"","text_to_display":"","show_title_field":"","pdf_file":"","pdf_file_button_styling":"","pdf_file_button_text":""}]

I need to convert it to a PHP array. I used following code to make it as an array.

$wpaf_field_title = maybe_unserialize(get_post_meta(52, '__wpaf_field_title', true));
print_r(json_encode($wpaf_field_title));

But it returns me

"[{\"field\":\"Email:1\",\"title\":\"email\",\"explanation_text\":\"\",\"explanation_text_location\":\"\",\"html_styling\":\"\",\"text_to_display\":\"\",\"show_title_field\":\"\",\"pdf_file\":\"\",\"pdf_file_button_styling\":\"\",\"pdf_file_button_text\":\"\"}]" 
2
  • use json_decode instead of json_encode Commented Nov 15, 2015 at 6:18
  • @user3384985 data is not the type of serialize it json_encode you need to use json_decode. Commented Nov 15, 2015 at 6:31

1 Answer 1

2

As said in comments you have to use json_decode() function:

$json = '[{"field":"Email:1","title":"email","explanation_text":"","explanation_text_location":"","html_styling":"","text_to_display":"","show_title_field":"","pdf_file":"","pdf_file_button_styling":"","pdf_file_button_text":""}]';
$data = json_decode( $json );
var_dump( $data );

And then you get:

array(1) {
  [0]=>
  object(stdClass)#1 (10) {
    ["field"]=>
    string(7) "Email:1"
    ["title"]=>
    string(5) "email"
    ["explanation_text"]=>
    string(0) ""
    ["explanation_text_location"]=>
    string(0) ""
    ["html_styling"]=>
    string(0) ""
    ["text_to_display"]=>
    string(0) ""
    ["show_title_field"]=>
    string(0) ""
    ["pdf_file"]=>
    string(0) ""
    ["pdf_file_button_styling"]=>
    string(0) ""
    ["pdf_file_button_text"]=>
    string(0) ""
  }
}
Sign up to request clarification or add additional context in comments.

Comments

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.