Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 检索数据库wordpress中的序列化值_Php_Mysql_Arrays_Wordpress_Serialization - Fatal编程技术网

Php 检索数据库wordpress中的序列化值

Php 检索数据库wordpress中的序列化值,php,mysql,arrays,wordpress,serialization,Php,Mysql,Arrays,Wordpress,Serialization,我试图在wordpress中查询数据库中的序列化数组值,该值将存储在表wp_postemta的meta_value列中 首先,我使用php的serialize()函数存储数组 那么比如说, $postID = 1; $arr = array(1, 2, 3); $ser_val = serialize($arr); update_meta_data($postID, '_customvalue', $ser_val); 存储的值是这样的 s:30:"a:3:{i:0;i:1;i:1;i:2;i

我试图在wordpress中查询数据库中的序列化数组值,该值将存储在表wp_postemta的meta_value列中
首先,我使用php的serialize()函数存储数组
那么比如说,

$postID = 1;
$arr = array(1, 2, 3);
$ser_val = serialize($arr);
update_meta_data($postID, '_customvalue', $ser_val);
存储的值是这样的

s:30:"a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}";
然后,当我试图通过执行wordpress sql查询来检索它时。。我所期望的是它将是一个数组,因为它存储为数组,但这样做之后,它将显示为字符串而不是数组

    $get_score = $wpdb->get_row("SELECT meta_value FROM wp_postmeta WHERE meta_key = '_cummulativescore'");
    $scr = unserialize($get_score->meta_value);
    var_dump($scr);

    //output displayed
    //string(30) "a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}" 
我确实使用is_array()函数检查了该值,结果是它不是数组

有没有办法将序列化值作为数组来获取

看起来您的数据在序列化过程中被转换为字符串

数据应存储为

a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}
而不是

s:30:"a:3:{i:0;i:1;i:1;i:2;i:2;i:3;}"

s:30表示字符串长度30。

那么您有什么建议?我是否应该在存储序列化数组时执行循环?不确定这是什么意思。我认为您的代码在某种程度上对数组执行了两次序列化。检查update_meta_data函数是否已经序列化,因此您可以跳过自己的序列化。嘿,您是对的。它正在执行两次序列化。现在该解决了。谢谢