PHP序列化字符串格式

时间:2015-11-22 02:40:44

标签: php mysql wordpress serialization

在MySQL(用于wordpress)中,post_metadata中有以下值:

a:1:{i:0;a:2:{s:5:"title";s:7:"Payment";s:19:"percentage_complete";s:1:"5";}}

我们这样做:

$value_percentage = array (
      0 => 
      array (
        'title' => 'Payment',
        'percentage_complete' => '15',
      ),
    );
$serializedData = serialize($value_percentage);
update_post_meta( $id, $field_percentage, $serializedData );

但是在DB中有:

s:78:"a:1:{i:0;a:2:{s:5:"title";s:7:"Payment";s:19:"percentage_complete";s:2:"15";}}";

如何避免s:78:"

我们还尝试了以下方法:

$value_percentage['percentage_complete'] = '15';

但是数据库中有s:78:"

1 个答案:

答案 0 :(得分:1)

您可以使用maybe serializemaybe_unserialize功能来存储和提取数据

可能是反序列化

$string = 'a:1:{i:0;a:2:{s:5:"title";s:7:"Payment";s:19:"percentage_complete";s:1:"5";}}';
echo '<pre>', print_r(maybe_unserialize($string), 1), '</pre>';

输出是这样的,

Array (
    [0] => Array (
            [title] => Payment
            [percentage_complete] => 5
        )

)

也许序列化

$value_percentage = array (
      0 => 
      array (
        'title' => 'Payment',
        'percentage_complete' => '15',
      ),
);
echo '<pre>', print_r(maybe_serialize($value_percentage), 1), '</pre>';

输出

a:1:{i:0;a:2:{s:5:"title";s:7:"Payment";s:19:"percentage_complete";s:2:"15";}}