doulu6314 2015-03-15 06:21
浏览 88
已采纳

如何在PHP中正确序列化和反序列化数组?

$array = array(
    'host_name' => array(
        'ip_add' => '127.0.0.1',
        'is_allow' => 0,
    ),
);
$str = serialize($array);

This value insert in my website. and read it using php function file_get_contents, I get this result from that page

a:1:{s:9:"host_name";a:2:{s:6:"ip_add";s:9:"127.0.0.1";s:8:"is_allow";i:0;}}

and try to unserialize it, but it show a notice like:-

Notice: unserialize(): Error at offset 5 of 116 bytes in /Applications/XAMPP/xamppfiles/htdocs/admin

  • 写回答

1条回答 默认 最新

  • dongqiongzheng0615 2015-03-15 06:40
    关注

    If you want unserialize you can use something like this:

    <?php
    
    $result = 'a:1:{s:9:"host_name";a:2:{s:6:"ip_add";s:9:"127.0.0.1";s:8:"is_allow";i:0;}}';
    
    $decoded = unserialize($result);
    
    print_r($decoded);
    
    ?>
    

    Is the same that:

    <?php
    
    $array = array('host_name' => array('ip_add' => '127.0.0.1', 'is_allow' => 0));
    $str = serialize($array);
    
    $decoded = unserialize($str);
    
    print_r($decoded);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题