php - 将 WordPress 帖子元转换为 PHP 数组

标签 php mysql arrays wordpress

我有一个像 MySql 这样的 Wordpress post 元

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

最佳答案

正如评论中所说,你必须使用 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) ""
  }
}

关于php - 将 WordPress 帖子元转换为 PHP 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33717004/

相关文章:

php - 在php中将数组添加到子数组

php - 如何在 PHP5 中构建多 oop 函数

javascript - 如何将事件的 css 类添加到 Wordpress 中的链接

PHP 每页显示 x 个结果的最佳方法

php - 需要帮助找出 MySQL 查询来计算某个数字

javascript - 当数据表输入来自服务器的 JSON 数据时更改 Google Chart 条形颜色

mysql - 在不同的列上加入同一个表两次

javascript - 从 JSON 对象中提取深层数据

java - 为什么我不能在 java 中传递一个短数组到接受 int 数组的方法?

arrays - Scala:如何指定暗示相等的类型参数边界?