php如何读取远程xml文件并转化为数组

发布时间:2021-10-27 10:08:02 作者:iii
来源:亿速云 阅读:127

这篇文章主要讲解了“php如何读取远程xml文件并转化为数组”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“php如何读取远程xml文件并转化为数组”吧!

php读取远程xml文件并转化为数组的方法:1、创建一个PHP示例文件;2、通过“simplexml_load_file”取得xml对象;3、循环把数据对象转化为数组即可。

php如何读取远程xml文件并转化为数组

本文操作环境:Windows7系统、PHP7.1版、DELL G3电脑

php怎么读取远程xml文件并转化为数组?

PHP读取远程xml文件并转化为数组:

远程文件如下:http://api.wycq.521g.com/game/mrt_consume/xyg ,打开是一个xml文件,内容是一个游戏的充值排名和不同名次奖励信息,需要取出前十名的信息和奖励信息用于展示,

实现代码如下:

public function index()
    {
        $url = "http://api.wycq.521g.com/game/mrt_consume/xyg";
        $contentxml = simplexml_load_file($url);  //取得xml对象,需要转化为数组
        $ranks = array();
        for($i=0;$i<=9;$i++)
        {
            $temp_player = (array)$contentxml->ranking->player[$i];  //循环把条排名数据对象转化为数组
            $ranks[$i+1] = $temp_player['@attributes'];    //取出数组中相应的属性值
        }
        $awards = array();
        for($i=0;$i<=4;$i++)
        {
            $awards[$i] = (array)$contentxml->awards->award[$i];  //循环把奖励数据对象转化为数组
        }
        $res_awards = array();
        foreach($awards as $k=>$v)
        {
            $res_awards[$k]["caption"] = $v['@attributes']["caption"];  //进一步处理把数组处理干净
            $res_awards[$k]["items"] = array();
            foreach((array)$v["items"] as $m =>$n)
            {
                $res_awards[$k]["items"] = $n;
            }
        }
        for($j=0;$j<=4;$j++)
        {
            foreach($res_awards[$j]["items"] as $p=>$q)
            {
                $temp_q = (array)$q;
                $res_awards[$j]["items"][$p] = $temp_q['@attributes'];
            }
        }
        $this->_data["ranks"] = $ranks;
        $this->_data["res_awards"]= $res_awards;
        $this->load->view('hd/wymingrentang/wymingrentang', $this->_data);  //指定view页输出数据
    }

感谢各位的阅读,以上就是“php如何读取远程xml文件并转化为数组”的内容了,经过本文的学习后,相信大家对php如何读取远程xml文件并转化为数组这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. 怎么使用php将json转化为数组
  2. php将json转化为数组的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php

上一篇:怎么手动配置node_modules中的依赖包

下一篇:Mysql数据分组排名实现的示例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》