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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?