PHP:从序列化数组中删除整个数组的字符串计数

PHP: Removing string count of entire array from serialized array

我有一个 JSON 数组,我将其保存在 PHP 变量中。然后我使用 serialize($variable) 序列化该数组,并使用内置的 Wordpress 函数 update_post_meta().

将其保存到 dB

我遇到的问题是整个序列化数组都用字符串计数包装。像这样,当前保存为:

s:332:"a:2:{i:0;a:7:{s:4:"type";s:5:"weeks";s:4:"cost";s:1:"3";s:8:"modifier";s:0:"";s:9:"base_cost";s:1:"2";s:13:"base_modifier";s:0:"";s:4:"from";s:1:"1";s:2:"to";s:1:"4";}i:1;a:7:{s:4:"type";s:7:"persons";s:4:"cost";s:1:"6";s:8:"modifier";s:0:"";s:9:"base_cost";s:1:"5";s:13:"base_modifier";s:0:"";s:4:"from";s:1:"1";s:2:"to";s:1:"2";}}";

我需要在没有整个数组的字符串计数的情况下保存它。期望的输出:

a:2:{i:0;a:7:{s:4:"type";s:5:"weeks";s:4:"cost";s:1:"3";s:8:"modifier";s:0:"";s:9:"base_cost";s:1:"2";s:13:"base_modifier";s:0:"";s:4:"from";s:1:"1";s:2:"to";s:1:"4";}i:1;a:7:{s:4:"type";s:7:"persons";s:4:"cost";s:1:"6";s:8:"modifier";s:0:"";s:9:"base_cost";s:1:"5";s:13:"base_modifier";s:0:"";s:4:"from";s:1:"1";s:2:"to";s:1:"2";}};

我们一如既往地非常感谢您提供的任何帮助。

你的数组似乎被序列化了两次,这就是给你的附录......我已经获取了你的序列化数据并对其进行了两次反序列化,它如你所愿地返回了:

<?php

$ser = 's:332:"a:2:{i:0;a:7:{s:4:"type";s:5:"weeks";s:4:"cost";s:1:"3";s:8:"modifier";s:0:"";s:9:"base_cost";s:1:"2";s:13:"base_modifier";s:0:"";s:4:"from";s:1:"1";s:2:"to";s:1:"4";}i:1;a:7:{s:4:"type";s:7:"persons";s:4:"cost";s:1:"6";s:8:"modifier";s:0:"";s:9:"base_cost";s:1:"5";s:13:"base_modifier";s:0:"";s:4:"from";s:1:"1";s:2:"to";s:1:"2";}}";';

$arr = unserialize($ser);
echo '<pre>';
print_r($arr); /* Print after one unserialize */
echo '<pre>';
print_r(unserialize($arr)); /* Print with unserialize to the once unserialized*/

会 return:

a:2:{i:0;a:7:{s:4:"type";s:5:"weeks";s:4:"cost";s:1:"3";s:8:"modifier";s:0:"";s:9:"base_cost";s:1:"2";s:13:"base_modifier";s:0:"";s:4:"from";s:1:"1";s:2:"to";s:1:"4";}i:1;a:7:{s:4:"type";s:7:"persons";s:4:"cost";s:1:"6";s:8:"modifier";s:0:"";s:9:"base_cost";s:1:"5";s:13:"base_modifier";s:0:"";s:4:"from";s:1:"1";s:2:"to";s:1:"2";}}
Array
(
    [0] => Array
        (
            [type] => weeks
            [cost] => 3
            [modifier] => 
            [base_cost] => 2
            [base_modifier] => 
            [from] => 1
            [to] => 4
        )

    [1] => Array
        (
            [type] => persons
            [cost] => 6
            [modifier] => 
            [base_cost] => 5
            [base_modifier] => 
            [from] => 1
            [to] => 2
        )

)

如您所见,仅在两次反序列化后才将其 return 返回数组...所以只需执行一次即可获得所需的内容。