PHP Foreach循环麻烦解析xml数据?

时间:2017-02-21 13:18:07

标签: php

我使用xml api。我使用simplexml_load_string方法解析了xml文件。但我也想循环作者数据。我遇到了使用foreach循环检索数据的问题。我怎么能解决这个问题。这是输出。 enter image description here

[0] => SimpleXMLElement Object
                (
                    [title] => Impact of Electron-Electron Cusp on Configuration Interaction Energies                 

                    [author] => Array
                        (
                            [0] => SimpleXMLElement Object
                                (
                                    [name] => David Prendergast
                                )

                            [1] => SimpleXMLElement Object
                                (
                                    [name] => M. Nolan
                                )

                            [2] => SimpleXMLElement Object
                                (
                                    [name] => Claudia Filippi
                                )

                            [3] => SimpleXMLElement Object
                                (
                                    [name] => Stephen Fahy
                                )

                            [4] => SimpleXMLElement Object
                                (
                                    [name] => J. C. Greer
                                )
                        )               
                )

            [1] => SimpleXMLElement Object
                (
                    [title] => Electron thermal conductivity owing to collisions between degenerate electrons
                    [author] => Array
                        (
                            [0] => SimpleXMLElement Object
                                (
                                    [name] => P. S. Shternin
                                )

                            [1] => SimpleXMLElement Object
                                (
                                    [name] => D. G. Yakovlev
                                )
                       )        
                )

apicall.php

class arxivApi{
    public $search_query = '';
    public $start = 0;
    public $max_results = 2;
    public $url = "http://export.arxiv.org/api/query?";

    public function query($term){

        $search_query = "all:".urlencode($term);
        $q = array();
        $param = array(    
                'search_query' => $search_query,
                'start'        => $this->start,
                'max_results'  => $this->max_results
            );
        foreach ($param as $key => $value) { $q[] = $key . '=' . $value; }
        $httpquery = implode('&',$q);
        $url = $this->url.$httpquery;
        $xml = $this->load_file_from_url($url);
        $result = $this->parse($xml);
    }

    public function load_file_from_url($url){

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $str = curl_exec($ch);
        $str = simplexml_load_string($str);         
        curl_close($ch);
        return $str;

    }


    public function parse($xml){    

            $data = array();            
            foreach ($xml->entry as $val) {    
                    $url = $val->id;    
                    $id = strrchr($url,"/");
                    $id = substr($id,1,strlen($id));    
                    $data[] = array(
                            'title' => (string)$val->title,
                            'id'    => (string)$id,
                            'summary' => (string)$val->summary
                        );          
                    $authors = array();
                        foreach($val->author as $sname){
                            $authors[] = (string)$sname->name;
                        }                       

                }
                $data[] = array(
                            'title' => (string)$val->title,
                            'id' => (string)$val->id,
                            'summary' => (string)$val->summary,
                            'authros' => implode(',',$authors)
                        );

            return $data;
    }
}
$result = new arxivApi();
$result->query('electron');

1 个答案:

答案 0 :(得分:0)

这是我的问题的解决方案。以前我没有正确获取作者数组值。对代码作者数组细节的一些小修改正确地进行了。 enter image description here

<强> problem.php

public function parse($xml){    

            $data = array();            
            foreach ($xml->entry as $val) {    
                    $url = $val->id;    
                    $id = strrchr($url,"/");
                    $id = substr($id,1,strlen($id));    
                    $data[] = array(
                            'title' => (string)$val->title,
                            'id'    => (string)$id,
                            'summary' => (string)$val->summary
                        );          
                    $authors = array();
                        foreach($val->author as $sname){
                            $authors[] = (string)$sname->name;
                        }                       

                }
                $data[] = array(
                            'title' => (string)$val->title,
                            'id' => (string)$val->id,
                            'summary' => (string)$val->summary,
                            'authros' => implode(',',$authors)
                        );

            return $data;
    }

<强> solution.php

public function parse($xml, $compact=false){


            $data = array();


            foreach ($xml->entry as $val) {

                if($compact){
                    //$url = $val->id;

                    // $id = strrchr($url,"/");
                    // $id = substr($id,1,strlen($id));

                    $data[] = array(
                            'title' => (string)$val->title,
                            'id'    => (string)$id,
                            'summary' => (string)$val->summary
                            //'author' => $val->author->name
                        );
                }else{
                    $authors = array();
                    if($val->author){
                        try {
                            foreach ($val->author as $k => $v) {
                                $authors[] = (string)$v->name;
                            }
                        } catch (Exception $e) {
                            $v = $val->author;
                            $authors[] = (string)$v->name;
                        }
                    }
                }

                    $data[] = array(
                            'title' => (string)$val->title,
                            'id'    => (string)$val->id,
                            'summary' => (string)$val->summary,
                            'author' => $authors
                        );                      
                }


            return $data;
    }