dongyan8929 2013-11-12 09:06
浏览 99
已采纳

Php - 创建并显示多维数组

I have this XML:

<eSummaryResult>
    <DocSum>
        <Id>11482001</Id>
        <Item Name="PubDate" Type="Date">2001 Jun</Item>
        <Item Name="EPubDate" Type="Date" />
        <Item Name="Source" Type="String">Adverse Drug React Toxicol Rev</Item>
        <Item Name="AuthorList" Type="List">
            <Item Name="Author" Type="String">Mantle D</Item>
            <Item Name="Author" Type="String">Gok MA</Item>
            <Item Name="Author" Type="String">Lennard TW</Item>
        </Item>
        <Item Name="LastAuthor" Type="String">Lennard TW</Item>
        <Item Name="Title" Type="String">Adverse and beneficial effects of plant extracts on skin and skin disorders.</Item>
        <Item Name="Volume" Type="String">20</Item>
        <Item Name="Issue" Type="String">2</Item>
        <Item Name="Pages" Type="String">89-103</Item>
        <Item Name="LangList" Type="List">
            <Item Name="Lang" Type="String">English</Item>
        </Item>
        <Item Name="NlmUniqueID" Type="String">9109474</Item>
        <Item Name="ISSN" Type="String">0964-198X</Item>
        <Item Name="ESSN" Type="String" />
        <Item Name="PubTypeList" Type="List">
            <Item Name="PubType" Type="String">Journal Article</Item>
            <Item Name="PubType" Type="String">Review</Item>
        </Item>
        <Item Name="RecordStatus" Type="String">PubMed - indexed for MEDLINE</Item>
        <Item Name="PubStatus" Type="String">ppublish</Item>
        <Item Name="ArticleIds" Type="List">
            <Item Name="pubmed" Type="String">11482001</Item>
            <Item Name="eid" Type="String">11482001</Item>
            <Item Name="rid" Type="String">11482001</Item>
        </Item>
        <Item Name="History" Type="List">
            <Item Name="pubmed" Type="Date">2001/08/03 10:00</Item>
            <Item Name="medline" Type="Date">2002/01/23 10:01</Item>
            <Item Name="entrez" Type="Date">2001/08/03 10:00</Item>
        </Item>
        <Item Name="References" Type="List" />
        <Item Name="HasAbstract" Type="Integer">1</Item>
        <Item Name="PmcRefCount" Type="Integer">3</Item>
        <Item Name="FullJournalName" Type="String">Adverse drug reactions and toxicological reviews</Item>
        <Item Name="ELocationID" Type="String" />
        <Item Name="SO" Type="String">2001 Jun;20(2):89-103</Item>
    </DocSum>
</eSummaryResult>

And i would like to create an array like this:

$results2 = array(
    '0' => array(
        'id' => 'xxxxxxxx',
        'authors' => array(
            '0' => 'xxxxxxxxxx',
            '1' => 'xxxxxxxxxx',
            )
    ),
    '1' => array(
        'id' => 'xxxxxxxx',
        'authors' => array(
            '0' => 'xxxxxxxxxx',
            '1' => 'xxxxxxxxxx',
            )
    ),
);

I have do this function to get data from XML to PHP in which i am trying to put the data with this structure and then display. I have not much experience with PHP i am having problems with syntax. This is the function:

function esummary_query($db, $id) { 
    $context = stream_context_create(array(
      'http'=>array(
        'user_agent' => 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11'
       )
    ));

    $xml = file_get_contents('http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=' . $db . '&id=' . $id, FALSE, $context);

    $xml_file = simplexml_load_string($xml);
    $results2 = array();
    foreach ( $xml_file->DocSum as $items ) {
        $results2[]['id'] = $items->Id;
        $authors = $xml_file->xpath("//Item[@Name='Author']");
        foreach ($authors as $author) {
            $results2[]['authors'][] = $author;
        }
    }
    return $results2;
}


echo'<h3>id:</h3>' . $results2[0]['id'] . "<br> author:" . $results2[0]['authors'];

But when i display it, the id is shown but the author's names don't. print_r display:

Array ( [0] => Array ( [id] => SimpleXMLElement Object ( [0] => 11850928 ) ) [1] => Array ( [authors] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [Name] => Author [Type] => String ) [0] => LoPresti PJ ) ) ) [2] => Array ( [authors] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [Name] => Author [Type] => String ) [0] => Hambrick GW Jr ) ) ) )

Thanks everybody!

  • 写回答

2条回答 默认 最新

  • doucheng3811 2013-11-12 09:26
    关注

    Add counter and use it as array key

    function esummary_query($db, $id) { 
        $context = stream_context_create(array(
          'http'=>array(
            'user_agent' => 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11'
           )
        ));
    
        $xml = file_get_contents('http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=' . $db . '&id=' . $id, FALSE, $context);
    
        $xml_file = simplexml_load_string($xml);
        $results2 = array();
        $c=0;
        foreach ( $xml_file->DocSum as $items ) {           
            $results2[$c]['id'] = (string)$items->Id;
            $authors = $xml_file->xpath("//Item[@Name='Author']");
            foreach ($authors as $author) {
                $results2[$c]['authors'][] = (string)$author;
            }
            $c++;
        }
        return $results2;
    }
    

    example output

    Array
    (
        [0] => Array
            (
                [id] => 11482001
                [authors] => Array
                    (
                        [0] => Mantle D
                        [1] => Gok MA
                        [2] => Lennard TW
                        [3] => Mantle D
                        [4] => Gok MA
                        [5] => Lennard TW
                    )
    
            )
    
        [1] => Array
            (
                [id] => 11482001
                [authors] => Array
                    (
                        [0] => Mantle D
                        [1] => Gok MA
                        [2] => Lennard TW
                        [3] => Mantle D
                        [4] => Gok MA
                        [5] => Lennard TW
                    )
    
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,