PHP在数组中取消序列化

时间:2016-10-31 21:19:06

标签: php arrays

我有一个具有以下值的数组

[0] => array(4) {
["sku"] => string(12) "WMS-M-VN-MRN"
["name"] => string(62) "Maroon V-neck Jumper"
["qty_ordered"] => string(6) "1.0000"
["product_options"] => string(533) "a:2:{s:15:"info_buyRequest";a:6:{s:4:"uenc";s:64:"aHR0cDovL2VtYmxlbWF0aWMuY28udWsvd21zLW0tdm4tbXJuLz9fX19TSUQ9VQ,,";s:7:"product";s:3:"780";s:8:"form_key";s:16:"gDXvCEtQOlRWihqc";s:15:"related_product";s:0:"";s:7:"options";a:1:{i:1970;s:5:"17201";}s:3:"qty";s:1:"1";}s:7:"options";a:1:{i:0;a:7:{s:5:"label";s:27:"PLEASE SELECT SIZE REQUIRED";s:5:"value";s:12:"36 inch (13)";s:11:"print_value";s:12:"36 inch (13)";s:9:"option_id";s:4:"1970";s:11:"option_type";s:9:"drop_down";s:12:"option_value";s:5:"17201";s:11:"custom_view";b:0;}}}"
}

现在有一种方法可以对" Product_options"进行反序列化。 in-line字段,无需拆分数组并重新构建它?

1 个答案:

答案 0 :(得分:1)

这就是方法。您只需将unserialize的结果重新分配给同一个数组项。

<?php

$array = [
    [
        "sku" => "WMS-M-VN-MRN",
        "name" => "Maroon V-neck Jumper",
        "qty_ordered" => "1.0000",
        "product_options" => 'a:2:{s:15:"info_buyRequest";a:6:{s:4:"uenc";s:64:"aHR0cDovL2VtYmxlbWF0aWMuY28udWsvd21zLW0tdm4tbXJuLz9fX19TSUQ9VQ,,";s:7:"product";s:3:"780";s:8:"form_key";s:16:"gDXvCEtQOlRWihqc";s:15:"related_product";s:0:"";s:7:"options";a:1:{i:1970;s:5:"17201";}s:3:"qty";s:1:"1";}s:7:"options";a:1:{i:0;a:7:{s:5:"label";s:27:"PLEASE SELECT SIZE REQUIRED";s:5:"value";s:12:"36 inch (13)";s:11:"print_value";s:12:"36 inch (13)";s:9:"option_id";s:4:"1970";s:11:"option_type";s:9:"drop_down";s:12:"option_value";s:5:"17201";s:11:"custom_view";b:0;}}}'
    ]
];

$array[0]["product_options"] = unserialize($array[0]["product_options"]);

echo "<pre>";
print_r($array);
echo "</pre>";

输出:

Array
(
    [0] => Array
        (
            [sku] => WMS-M-VN-MRN
            [name] => Maroon V-neck Jumper
            [qty_ordered] => 1.0000
            [product_options] => Array
                (
                    [info_buyRequest] => Array
                        (
                            [uenc] => aHR0cDovL2VtYmxlbWF0aWMuY28udWsvd21zLW0tdm4tbXJuLz9fX19TSUQ9VQ,,
                            [product] => 780
                            [form_key] => gDXvCEtQOlRWihqc
                            [related_product] => 
                            [options] => Array
                                (
                                    [1970] => 17201
                                )

                            [qty] => 1
                        )

                    [options] => Array
                        (
                            [0] => Array
                                (
                                    [label] => PLEASE SELECT SIZE REQUIRED
                                    [value] => 36 inch (13)
                                    [print_value] => 36 inch (13)
                                    [option_id] => 1970
                                    [option_type] => drop_down
                                    [option_value] => 17201
                                    [custom_view] => 
                                )

                        )

                )

        )

)