Thierry Bonnechere
Thierry Bonnechere

Reputation: 103

PHP serialize string format

In MySQL (for wordpress) there is the following value in a post_metadata :

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

We do :

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

But in the DB there is :

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

How to avoid the s:78:" ?

We also tried the following :

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

but there is the s:78:" in the DB

Upvotes: 1

Views: 212

Answers (1)

silver
silver

Reputation: 5311

You can use maybe serialize and maybe_unserialize function to store and pull data

maybe 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>';

output is like this,

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

)

maybe serialize

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

output is

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

Upvotes: 1

Related Questions