dpmopn8542 2018-11-08 15:57
浏览 81
已采纳

PHP反序列化变量

Good afternoon everyone,

Is any simple way to unserialize that type of data and put everyone into Array()

$variable= "'Body Style'=>'SEDAN 4 DOOR';'CD Player'=>'PRESENT';'Color'=>'GRAY';'Engine Size'=>'3.0L V6 FI  F';'Air Bags'=>'4';'Radio'=>'PRESENT';'Tape Deck'=>'N/A';'Estimated Repair Cost'=>'5518';'ACV'=>'6122';'Driver Airbag'=>'INTACT';'Passenger Airbag'=>'INTACT';'Left Side Airbag'=>'INTACT';'Right Side Airbag'=>'INTACT';'VIN Status'=>'OK';'Interior Color'=>'GRAY';'Airbag'=>'INTACT';'KeyFob'=>'PRESENT';";

without using explode(), foreach() and then preg_match functions?

Thank you for answer.

  • 写回答

2条回答 默认 最新

  • dongse5408 2018-11-08 17:19
    关注

    Is any simple way to unserialize that type of data and put everyone into Array() without using using explode(), foreach() and then preg_match functions?

    Yes, you can use preg_match_all().

    if (preg_match_all("/'(.*?)'=>'(.*?)';/", $variable, $matches)) {
        $result = array_combine($matches[1], $matches[2]);
        print_r($result);
    }
    

    Output:

    Array
    (
        [Body Style] => SEDAN 4 DOOR
        [CD Player] => PRESENT
        [Color] => GRAY
        [Engine Size] => 3.0L V6 FI  F
        [Air Bags] => 4
        [Radio] => PRESENT
        [Tape Deck] => N/A
        [Estimated Repair Cost] => 5518
        [ACV] => 6122
        [Driver Airbag] => INTACT
        [Passenger Airbag] => INTACT
        [Left Side Airbag] => INTACT
        [Right Side Airbag] => INTACT
        [VIN Status] => OK
        [Interior Color] => GRAY
        [Airbag] => INTACT
        [KeyFob] => PRESENT
    )
    

    The advantage of this approach is it works even if any key or value contains semicolons such as Body;Style or SEDAN;4 DOOR which would make the explode() approach fail.

    See https://3v4l.org/YuuZM

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊