遠(yuǎn)程文件如下:http://api.wycq./game/mrt_consume/xyg ,,打開是一個xml文件,內(nèi)容是一個游戲的充值排名和不同名次獎勵信息,,需要取出前十名的信息和獎勵信息用于展示,
實現(xiàn)代碼如下:
- public function index()
- {
- $url = "http://api.wycq./game/mrt_consume/xyg";
- $contentxml = simplexml_load_file($url); //取得xml對象,,需要轉(zhuǎn)化為數(shù)組
- $ranks = array();
- for($i=0;$i<=9;$i++)
- {
- $temp_player = (array)$contentxml->ranking->player[$i]; //循環(huán)把條排名數(shù)據(jù)對象轉(zhuǎn)化為數(shù)組
- $ranks[$i+1] = $temp_player['@attributes']; //取出數(shù)組中相應(yīng)的屬性值
- }
- $awards = array();
- for($i=0;$i<=4;$i++)
- {
- $awards[$i] = (array)$contentxml->awards->award[$i]; //循環(huán)把獎勵數(shù)據(jù)對象轉(zhuǎn)化為數(shù)組
- }
- $res_awards = array();
- foreach($awards as $k=>$v)
- {
- $res_awards[$k]["caption"] = $v['@attributes']["caption"]; //進(jìn)一步處理把數(shù)組處理干凈
- $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頁輸出數(shù)據(jù)
- }
|