首页 >后端开发 >php教程 >Uncaught exception 'Exception' with message 'Serialization of 'SimpleXMLElement

Uncaught exception 'Exception' with message 'Serialization of 'SimpleXMLElement

WBOY
WBOY原创
2016-06-23 14:13:291850浏览

function deal_taobao_data($data){
    $data = serialize($data);
    $data = str_replace('O:16:"SimpleXMLElement"', 'a', $data);
    $arrstr = unserialize($data);
    return $arrstr;
}
这是我的代码
处理一段
SimpleXMLElement Object
(
    [item_cats] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [list] => true
                )

            [item_cat] => Array
                (

                ……
            )
    )
)
将其转化为数组
本地测试可以,上传后报错误:
Fatal error: Uncaught exception 'Exception' with message 'Serialization 
of 'SimpleXMLElement' is not allowed' in /web/taobao.php:328
Stack trace:
#0 /web/taobao.php(328): serialize(Object(SimpleXMLElement))
#1 /web/taobao.php(50): deal_taobao_data(Object(SimpleXMLElement))
#2 {main}
  thrown in /web/taobao.php on line 328
有没有朋友知道这一问题根源的。


回复讨论(解决方案)

本地测试可以???
开玩笑不怪你

确实测试无误。。。

本地测试可以???
开玩笑不怪你
返回了完整数组。

贴出你的示例数据

贴出你的示例数据
SimpleXMLElement Object
(
    [item_cats] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [list] => true
                )

            [item_cat] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [cid] => 50013865
                            [is_parent] => false
                            [name] => 项链
                            [parent_cid] => 50013864
                        )

                    [1] => SimpleXMLElement Object
                        (
                            [cid] => 50013868
                            [is_parent] => false
                            [name] => 项坠/吊坠
                            [parent_cid] => 50013864
                        )

                    [2] => SimpleXMLElement Object
                        (
                            [cid] => 50014227
                            [is_parent] => true
                            [name] => 耳饰
                            [parent_cid] => 50013864
                        )

                    [3] => SimpleXMLElement Object
                        (
                            [cid] => 50013869
                            [is_parent] => false
                            [name] => 手链
                            [parent_cid] => 50013864
                        )

                    [4] => SimpleXMLElement Object
                        (
                            [cid] => 50013870
                            [is_parent] => false
                            [name] => 手镯
                            [parent_cid] => 50013864
                        )

                    [5] => SimpleXMLElement Object
                        (
                            [cid] => 50013871
                            [is_parent] => false
                            [name] => 脚链
                            [parent_cid] => 50013864
                        )

                    [6] => SimpleXMLElement Object
                        (
                            [cid] => 50013875
                            [is_parent] => false
                            [name] => 戒指/指环
                            [parent_cid] => 50013864
                        )

                    [7] => SimpleXMLElement Object
                        (
                            [cid] => 50013876
                            [is_parent] => false
                            [name] => 胸针
                            [parent_cid] => 50013864
                        )

                    [8] => SimpleXMLElement Object
                        (
                            [cid] => 50013877
                            [is_parent] => false
                            [name] => 摆件
                            [parent_cid] => 50013864
                        )

                    [9] => SimpleXMLElement Object
                        (
                            [cid] => 50013878
                            [is_parent] => false
                            [name] => 发饰
                            [parent_cid] => 50013864
                        )

                    [10] => SimpleXMLElement Object
                        (
                            [cid] => 50013879
                            [is_parent] => false
                            [name] => DIY饰品配件
                            [parent_cid] => 50013864
                        )

                    [11] => SimpleXMLElement Object
                        (
                            [cid] => 50013880
                            [is_parent] => false
                            [name] => 首饰保养鉴定
                            [parent_cid] => 50013864
                        )

                    [12] => SimpleXMLElement Object
                        (
                            [cid] => 50013881
                            [is_parent] => false
                            [name] => 首饰盒/展示架
                            [parent_cid] => 50013864
                        )

                    [13] => SimpleXMLElement Object
                        (
                            [cid] => 50013882
                            [is_parent] => false
                            [name] => 其它首饰
                            [parent_cid] => 50013864
                        )

                )

        )

)
1

本地测试成功返回的代码(省略了一部分):
Array
(
    [item_cats] => Array
        (
            [@attributes] => Array
                (
                    [list] => true
                )

            [item_cat] => Array
                (
                    [0] => Array
                        (
                            [cid] => 50013865
                            [is_parent] => false
                            [name] => 项链
                            [parent_cid] => 50013864
                        )

                    [1] => Array
                        (
                            [cid] => 50013868
                            [is_parent] => false
                            [name] => 项坠/吊坠
                            [parent_cid] => 50013864
                        )

                    [2] => Array
                        (
                            [cid] => 50014227
                            [is_parent] => true
                            [name] => 耳饰
                            [parent_cid] => 50013864
                        )

                    [3] => Array
                        (
                            [cid] => 50013869
                            [is_parent] => false
                            [name] => 手链
                            [parent_cid] => 50013864
                        )


                )

        )

)
1

如果有能够代替的代码,也可以,能解决问题就行。

1、SimpleXMLElement 不可被序列化
Uncaught exception 'Exception' with message 'Serialization of 'SimpleXMLElement' is not allowed' 
2、即便是可以,由
echo serialize(array(1));
a:1:{i:0;i:1;}
也不是像你那样替换

1、SimpleXMLElement 不可被序列化
Uncaught exception 'Exception' with message 'Serialization of 'SimpleXMLElement' is not allowed' 
2、即便是可以,由
echo serialize(array(1));
a:1:{i:0;i:1;}
也不是像你那样替换
自己解决了,换一个函数!
function deal_taobao_data($data){
$arrstrs=json_encode($data);
$arrstr=json_decode($arrstrs,true);
return $arrstr;
}

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn