Php 将字符串中的数组存储为数组

Php 将字符串中的数组存储为数组,php,wordpress,Php,Wordpress,在wp_posts WordPress表中,我为post自定义post类型存储了一行,该post自定义post类型在post_content列中字面上存储了以下内容: 当我通过以下方式查询表时: $query = $wpdb->get_results( 'SELECT * FROM wp_posts where post_type = "custom_post_type"' ); foreach($query as $q) { $content = $q->post_con

在wp_posts WordPress表中,我为post自定义post类型存储了一行,该post自定义post类型在post_content列中字面上存储了以下内容:

当我通过以下方式查询表时:

$query = $wpdb->get_results( 'SELECT * FROM wp_posts where post_type = "custom_post_type"' );

foreach($query as $q) {
    $content = $q->post_content;
    var_dump($content);
}
上述每个变量的var_转储结果显然如下所示:

string(1369) string(1311) 
等等,因为post_content字段包含我试图提取的数组,该字段目前基本上保存为字符串

我知道我可以剥离/过滤掉方括号、方括号等,并循环遍历每个实例,以便直接从post_内容字段值创建关联数组,这很好

我的问题是,我是否错过了一个技巧,我可以通过简单地将字符串分配给变量来立即将其分配为数组?例如,使用上述方法:

foreach($query as $q) {
    $content = $q->post_content;
    var_dump($content);
}
结果是,每个var_转储都应该理想地打印出来:

array(2) { 'te' => string(4) "werg" 'wreg' => string(6) "werefg" }

注意-字段值只包含数组,没有任何附加或前缀…

看起来您可能想使用PHP方法,该方法将返回数组的字符串表示形式,然后可以存储在数据库中。 然后可以使用再次从字符串中取出数据


如果您迫切需要将var_dump输出重新分析为数组,您可以查看此答案,但它并不完美。

如果我理解您的问题,您可以序列化数组,同时将数据保存在post_内容字段中,然后在渲染时将其取消序列化。但是最好的选择是使用Posteta表来满足您的定制需求。谢谢您的回答-最后我使用了serialize,还有其他一些东西!
array(2) { 'te' => string(4) "werg" 'wreg' => string(6) "werefg" }