2015-11-15 47 views
0

我有一個WordPress郵寄元到MySql像轉換WordPress郵寄元到PHP數組

[{"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":""}] 

我需要將其轉換爲PHP陣列。我使用下面的代碼來使它成爲一個數組。

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

但它返回我

"[{\"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\":\"\"}]" 
+0

使用json_decode代替json_encode – vel

+0

@ user3384 985數據不是它的'json_encode'序列化類型,你需要使用'json_decode'。 – Noman

回答

2

正如評論說,你必須使用json_decode()函數:

$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); 

然後你會得到:

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) "" 
    } 
}