在php中反序列化序列化数组

时间:2013-03-06 07:25:31

标签: php serialization

我有一个数组,其中所有键值都是序列化方式,这里是示例代码

Array
(
    [Entry1] => Array
    (
        [emergency_contact] => s:205:"a:5:{s:22:"emergency_contact_name";s:0:"";s:23:"emergency_contact_email";s:0:"";s:23:"emergency_contact_phone";s:0:"";s:26:"emergency_contact_relation";s:0:"";s:30:"emergency_contact_rx_allergies";s:0:"";}";
        [email] => s:23:"h.v@gmail.com";
        [c_address] => s:14:"1067 Broad Way";
        [c_address2] => s:6:"Mangal";
        [c_postcode] => s:5:"12345";
        [c_city] => s:14:"Sans Frnasisco";
        [c_state] => s:7:"LOS";
        [c_country_of_residence] => s:3:"109";
    )
    [Entry2] => Array
    (
        [emergency_contact] => s:205:"a:5:{s:22:"emergency_contact_name";s:0:"";s:23:"emergency_contact_email";s:0:"";s:23:"emergency_contact_phone";s:0:"";s:26:"emergency_contact_relation";s:0:"";s:30:"emergency_contact_rx_allergies";s:0:"";}";
        [email] => s:23:"h.v@gmail.com";
        [c_address] => s:14:"1067 Broad Way";
        [c_address2] => s:6:"Mangal";
        [c_postcode] => s:5:"12345";
        [c_city] => s:14:"Sans Frnasisco";
        [c_state] => s:7:"LOS";
        [c_country_of_residence] => s:3:"109";
    )
)

有什么方法可以查看所有数据更改它并再次以相同方式再次序列化?感谢。

1 个答案:

答案 0 :(得分:1)

array_walk_recursive($array, function (&$value) { $value = unserialize($value); });

// do something with the array

array_walk_recursive($array, function (&$value) { $value = serialize($value); });

但实际上,将每个单独的值序列化是非常疯狂的。如果可以,你应该停止。