dounao4179 2017-08-07 13:44
浏览 80
已采纳

将序列化数据从数据库转换为PHP数组

I've got some custom data inputted into the postmeta table for each of my WooCommerce orders.

a:2:{i:6517;a:2:{i:0;a:5:{s:10:"first_name";s:5:"Roger";s:9:"last_name";s:6:"Rabbit";s:5:"email";s:19:"roger@benefacto.org";s:7:"is_lead";b:1;s:12:"is_cancelled";b:0;}i:1;a:5:{s:10:"first_name";s:7:"Jessica";s:9:"last_name";s:6:"Rabbit";s:5:"email";s:21:"Jessica@benefacto.org";s:7:"is_lead";b:0;s:12:"is_cancelled";b:0;}}i:6518;a:2:{i:0;a:5:{s:10:"first_name";s:6:"Mickey";s:9:"last_name";s:5:"Mouse";s:5:"email";s:20:"mickey@benefacto.org";s:7:"is_lead";b:0;s:12:"is_cancelled";b:0;}i:1;a:5:{s:10:"first_name";s:6:"Donald";s:9:"last_name";s:4:"Duck";s:5:"email";s:20:"donald@benefacto.org";s:7:"is_lead";b:0;s:12:"is_cancelled";b:0;}}}

-- EDIT ---

I initially assumed this was encoded with JSON, but following the answers below understand it is actually serialized data.

I want to convert this data into a PHP array - something like below - so I can play with it.

 $data = array ( 
    '6517' => array ( 
        0 => array ( 
            'first_name' => 'Roger',
            'last_name' => 'Rabit',
            'email' => 'roger@benefacto.org',
            'is_lead' => 1,
            'is_cancelled' => 0,
        ),
        1 => array (
            'first_name' => 'Jessica',
            'last_name' => 'Rabit',
            'email' => 'jessica@benefacto.org',
            'is_lead' => 0,
            'is_cancelled' => 0,
        )
    ),
    '6518' => array (
        0 => array (
            'first_name' => 'Mickey',
            'last_name' => 'Mouse',
            'email' => 'mickey@benefacto.org',
            'is_lead' => 1,
            'is_cancelled' => 0,
        ),
        1 => array (
            'first_name' => 'Donald',
            'last_name' => 'Duck',
            'email' => 'donald@benefacto.org',
            'is_lead' => 0,
            'is_cancelled' => 0,
        )
    )
);

Any thoughts much appreciated!

  • 写回答

1条回答 默认 最新

  • dongsui8162 2017-08-07 13:57
    关注

    As per the comments above this data is serialized rather than JSON encoded. For anyone else reading, the following works well.

    // Pull serialized data 
    $serializeddata = 'a:2:{i:6517;a:2:{i:0;a:5:{s:10:"first_name";s:5:"Roger";s:9:"last_name";s:6:"Rabbit";s:5:"email";s:19:"roger@benefacto.org";s:7:"is_lead";b:1;s:12:"is_cancelled";b:0;}i:1;a:5:{s:10:"first_name";s:7:"Jessica";s:9:"last_name";s:6:"Rabbit";s:5:"email";s:21:"Jessica@benefacto.org";s:7:"is_lead";b:0;s:12:"is_cancelled";b:0;}}i:6518;a:2:{i:0;a:5:{s:10:"first_name";s:6:"Mickey";s:9:"last_name";s:5:"Mouse";s:5:"email";s:20:"mickey@benefacto.org";s:7:"is_lead";b:0;s:12:"is_cancelled";b:0;}i:1;a:5:{s:10:"first_name";s:6:"Donald";s:9:"last_name";s:4:"Duck";s:5:"email";s:20:"donald@benefacto.org";s:7:"is_lead";b:0;s:12:"is_cancelled";b:0;}}}';
    
    // Unserialize it into a standard array
    $array = unserialize($serializeddata);
    
    // Print Array
    print_r($array);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化