您的位置:首页 > 其它

xml数据转数组

2016-06-18 16:48 337 查看
xml数据转化数组

$xml = file_get_contents("abc.xml");

    $ob= simplexml_load_string($xml);  //将xml数据转换成对象

    $objson = json_encode($ob);

    $objson = str_replace('<','<',$objson);

    $obarr = json_decode($objson,true);

    function d($obarr){

        foreach($obarr as $k => $v){

            if(is_array($v)){

                foreach($v as $key => $val){

                    if($key=='@attributes'){

                        unset($obarr[$k]['@attributes']);

                    }if($key=='item'){

                        foreach($val as $keyy => $vall){

                            if($keyy=='@attributes'){

                                unset($obarr[$k]['item']['@attributes']);

                            }if($keyy=='item'){

                            }

                        }

                    }

                }

            }

        }

        return $obarr;

    }

    $s = d($obarr['item']);

    $s['name'] = "zhangsan's";

    $s['sex'] = "man";

    echo "<pre>";

    print_r($s);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: