dongling2038 2016-10-31 21:19
浏览 41
已采纳

PHP在数组中反序列化

I have an array that has the following values

[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;}}}"
}

Now is there a way to unserialise the "Product_options" field in-line, without having to break the array apart and rebuild it again?

  • 写回答

1条回答 默认 最新

  • donglei7152 2016-10-31 21:27
    关注

    This is the way. You just have to reassign the result of unserialize to the same array item.

    <?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>";
    

    Output:

    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] => 
                                    )
    
                            )
    
                    )
    
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图