2010-12-17 78 views
-2

我需要从这个simplexmlobject去:阵列子集PHP

SimpleXMLElement Object 
(
    [PubmedArticle] => Array 
     (
      [0] => SimpleXMLElement Object 
       (
        [MedlineCitation] => SimpleXMLElement Object 
         (
          [@attributes] => Array 
           (
            [Owner] => NLM 
            [Status] => In-Process 
           ) 

          [PMID] => 20538400 
          [DateCreated] => SimpleXMLElement Object 
           (
            [Year] => 2010 
            [Month] => 07 
            [Day] => 08 
           ) 

          [Article] => SimpleXMLElement Object 
           (
            [@attributes] => Array 
             (
              [PubModel] => Print-Electronic 
             ) 

            [Journal] => SimpleXMLElement Object 
             (
              [ISSN] => 1090-2147 
              [JournalIssue] => SimpleXMLElement Object 
               (
                [@attributes] => Array 
                 (
                  [CitedMedium] => Internet 
                 ) 

                [Volume] => 73 
                [Issue] => 3 
                [PubDate] => SimpleXMLElement Object 
                 (
                  [Year] => 2010 
                  [Month] => Aug 
                 ) 

               ) 

              [Title] => Brain and cognition 
              [ISOAbbreviation] => Brain Cogn 
             ) 

            [ArticleTitle] => Increased saccadic rate during smooth pursuit eye movements in patients at Ultra High Risk for developing a psychosis. 
            [Pagination] => SimpleXMLElement Object 
             (
              [MedlinePgn] => 215-21 
             ) 

            [Abstract] => SimpleXMLElement Object 
             (
              [AbstractText] => Abnormalities in eye tracking are consistently observed in schizophrenia patients and their relatives and have been proposed as an endophenotype of the disease. The aim of this study was to investigate the performance of patients at Ultra High Risk (UHR) for developing psychosis on a task of smooth pursuit eye movement (SPEM). Forty-six UHR patients and twenty-eight age and education matched controls were assessed with a task of SPEM and psychiatric questionnaires. Our results showed that both the corrective and non-corrective saccadic rates during pursuit were higher in the UHR group. There were however no differences in smooth pursuit gain between the two groups. The saccadic rate was related to positive UHR symptoms. Our findings indicate that abnormalities in SPEM are already present in UHR patients, prior to a first psychotic episode. These abnormalities occur only in the saccadic system. 
              [CopyrightInformation] => 2010 Elsevier Inc. All rights reserved. 
             ) 

            [Affiliation] => Department of Psychiatry, Academic Medical Center, University of Amsterdam, The Netherlands. m.j.van[email protected] 
            [AuthorList] => SimpleXMLElement Object 
             (
              [@attributes] => Array 
               (
                [CompleteYN] => Y 
               ) 

              [Author] => Array 
               (
                [0] => SimpleXMLElement Object 
                 (
                  [@attributes] => Array 
                   (
                    [ValidYN] => Y 
                   ) 

                  [LastName] => van Tricht 
                  [ForeName] => M J 
                  [Initials] => MJ 
                 ) 

                [1] => SimpleXMLElement Object 
                 (
                  [@attributes] => Array 
                   (
                    [ValidYN] => Y 
                   ) 

                  [LastName] => Nieman 
                  [ForeName] => D H 
                  [Initials] => DH 
                 ) 

                [2] => SimpleXMLElement Object 
                 (
                  [@attributes] => Array 
                   (
                    [ValidYN] => Y 
                   ) 

                  [LastName] => Bour 
                  [ForeName] => L J 
                  [Initials] => LJ 
                 ) 

                [3] => SimpleXMLElement Object 
                 (
                  [@attributes] => Array 
                   (
                    [ValidYN] => Y 
                   ) 

                  [LastName] => Boerée 
                  [ForeName] => T 
                  [Initials] => T 
                 ) 

                [4] => SimpleXMLElement Object 
                 (
                  [@attributes] => Array 
                   (
                    [ValidYN] => Y 
                   ) 

                  [LastName] => Koelman 
                  [ForeName] => J H T M 
                  [Initials] => JH 
                 ) 

                [5] => SimpleXMLElement Object 
                 (
                  [@attributes] => Array 
                   (
                    [ValidYN] => Y 
                   ) 

                  [LastName] => de Haan 
                  [ForeName] => L 
                  [Initials] => L 
                 ) 

                [6] => SimpleXMLElement Object 
                 (
                  [@attributes] => Array 
                   (
                    [ValidYN] => Y 
                   ) 

                  [LastName] => Linszen 
                  [ForeName] => D H 
                  [Initials] => DH 
                 ) 

               ) 

             ) 

            [Language] => eng 
            [PublicationTypeList] => SimpleXMLElement Object 
             (
              [PublicationType] => Journal Article 
             ) 

            [ArticleDate] => SimpleXMLElement Object 
             (
              [@attributes] => Array 
               (
                [DateType] => Electronic 
               ) 

              [Year] => 2010 
              [Month] => 06 
              [Day] => 09 
             ) 

           ) 

          [MedlineJournalInfo] => SimpleXMLElement Object 
           (
            [Country] => United States 
            [MedlineTA] => Brain Cogn 
            [NlmUniqueID] => 8218014 
            [ISSNLinking] => 0278-2626 
           ) 

          [CitationSubset] => IM 
         ) 

        [PubmedData] => SimpleXMLElement Object 
         (
          [History] => SimpleXMLElement Object 
           (
            [PubMedPubDate] => Array 
             (
              [0] => SimpleXMLElement Object 
               (
                [@attributes] => Array 
                 (
                  [PubStatus] => received 
                 ) 

                [Year] => 2009 
                [Month] => 10 
                [Day] => 26 
               ) 

              [1] => SimpleXMLElement Object 
               (
                [@attributes] => Array 
                 (
                  [PubStatus] => revised 
                 ) 

                [Year] => 2010 
                [Month] => 4 
                [Day] => 28 
               ) 

              [2] => SimpleXMLElement Object 
               (
                [@attributes] => Array 
                 (
                  [PubStatus] => accepted 
                 ) 

                [Year] => 2010 
                [Month] => 5 
                [Day] => 12 
               ) 

              [3] => SimpleXMLElement Object 
               (
                [@attributes] => Array 
                 (
                  [PubStatus] => aheadofprint 
                 ) 

                [Year] => 2010 
                [Month] => 6 
                [Day] => 9 
               ) 

              [4] => SimpleXMLElement Object 
               (
                [@attributes] => Array 
                 (
                  [PubStatus] => entrez 
                 ) 

                [Year] => 2010 
                [Month] => 6 
                [Day] => 12 
                [Hour] => 6 
                [Minute] => 0 
               ) 

              [5] => SimpleXMLElement Object 
               (
                [@attributes] => Array 
                 (
                  [PubStatus] => pubmed 
                 ) 

                [Year] => 2010 
                [Month] => 6 
                [Day] => 12 
                [Hour] => 6 
                [Minute] => 0 
               ) 

              [6] => SimpleXMLElement Object 
               (
                [@attributes] => Array 
                 (
                  [PubStatus] => medline 
                 ) 

                [Year] => 2010 
                [Month] => 6 
                [Day] => 12 
                [Hour] => 6 
                [Minute] => 0 
               ) 

             ) 

           ) 

          [PublicationStatus] => ppublish 
          [ArticleIdList] => SimpleXMLElement Object 
           (
            [ArticleId] => Array 
             (
              [0] => S0278-2626(10)00059-X 
              [1] => 10.1016/j.bandc.2010.05.005 
              [2] => 20538400 
             ) 

           ) 

         ) 

       ) 

      [1] => SimpleXMLElement Object 
       (
        [MedlineCitation] => SimpleXMLElement Object 
         (
          [@attributes] => Array 
           (
            [Owner] => NLM 
            [Status] => MEDLINE 
           ) 

          [PMID] => 19109059 
          [DateCreated] => SimpleXMLElement Object 
           (
            [Year] => 2009 
            [Month] => 02 
            [Day] => 09 
           ) 

          [DateCompleted] => SimpleXMLElement Object 
           (
            [Year] => 2009 
            [Month] => 04 
            [Day] => 08 
           ) 

          [Article] => SimpleXMLElement Object 
           (
            [@attributes] => Array 
             (
              [PubModel] => Print-Electronic 
             ) 

            [Journal] => SimpleXMLElement Object 
             (
              [ISSN] => 1872-8952 
              [JournalIssue] => SimpleXMLElement Object 
               (
                [@attributes] => Array 
                 (
                  [CitedMedium] => Internet 
                 ) 

                [Volume] => 120 
                [Issue] => 2 
                [PubDate] => SimpleXMLElement Object 
                 (
                  [Year] => 2009 
                  [Month] => Feb 
                 ) 

               ) 

              [Title] => Clinical neurophysiology : official journal of the International Federation of Clinical Neurophysiology 
              [ISOAbbreviation] => Clin Neurophysiol 
             ) 

            [ArticleTitle] => Quantification of the auditory startle reflex in children. 
            [Pagination] => SimpleXMLElement Object 
             (
              [MedlinePgn] => 424-30 
             ) 

            [Abstract] => SimpleXMLElement Object 
             (
              [AbstractText] => Array 
               (

               ) 

             ) 


            [AuthorList] => SimpleXMLElement Object 
             (
              [@attributes] => Array 
               (
                [CompleteYN] => Y 
               ) 

              [Author] => Array 
               (
                [0] => SimpleXMLElement Object 
                 (
                  [@attributes] => Array 
                   (
                    [ValidYN] => Y 
                   ) 

                  [LastName] => Bakker 
                  [ForeName] => Mirte J 
                  [Initials] => MJ 
                 ) 

                [1] => SimpleXMLElement Object 
                 (
                  [@attributes] => Array 
                   (
                    [ValidYN] => Y 
                   ) 

                  [LastName] => Boer 
                  [ForeName] => Frits 
                  [Initials] => F 
                 ) 

                [2] => SimpleXMLElement Object 
                 (
                  [@attributes] => Array 
                   (
                    [ValidYN] => Y 
                   ) 

                  [LastName] => van der Meer 
                  [ForeName] => Johan N 
                  [Initials] => JN 
                 ) 

                [3] => SimpleXMLElement Object 
                 (
                  [@attributes] => Array 
                   (
                    [ValidYN] => Y 
                   ) 

                  [LastName] => Koelman 
                  [ForeName] => Johannes H T M 
                  [Initials] => JH 
                 ) 

                [4] => SimpleXMLElement Object 
                 (
                  [@attributes] => Array 
                   (
                    [ValidYN] => Y 
                   ) 

                  [LastName] => Boerée 
                  [ForeName] => Thijs 
                  [Initials] => T 
                 ) 

                [5] => SimpleXMLElement Object 
                 (
                  [@attributes] => Array 
                   (
                    [ValidYN] => Y 
                   ) 

                  [LastName] => Bour 
                  [ForeName] => Lo 
                  [Initials] => L 
                 ) 

                [6] => SimpleXMLElement Object 
                 (
                  [@attributes] => Array 
                   (
                    [ValidYN] => Y 
                   ) 

                  [LastName] => Tijssen 
                  [ForeName] => Marina A J 
                  [Initials] => MA 

在一个磁盘阵列,看起来像:

Lastname (number of times) 
Boerée (2) 
Koelman (2) 
Bour (2) 
van Tricht (1) 
Nieman (1) 
de Haan (1) 
Linzen (1) 
Bakker (1) 
Boer (1) 
Tijssen (1) 
van der Meer (1) 

商祺!

我需要做的第一件事是让所有的Last_names在一个阵列中,这应该有一个foreach循环来完成:

foreach($xml->xpath('//AuthorList') as $AuthorList) { 
    foreach ($AuthorList->Author as $Author) { 
     //$author_last_name_list = $Author->LastName; 
     //$author_last_name_list .= $author_last_name_list; 
     $author_last_name_list join array($Author->LastName); 
    } 
} 

(这不起作用)

但我怎么在foreach循环中构建一个数组?第二,如果我有大名单,我如何得到一个新的阵列“名称在阵列中多少次”...问候!

+3

你忘了输入你的问题。 – BoltClock 2010-12-17 08:59:45

+0

我需要做的第一件事是在一个数组中获得所有的Last_names,这应该用foreach循环完成:foreach($ xml-> xpath('// AuthorList')作为$ AuthorList) { foreach($ AuthorList - >作者$作者) { // $ author_last_name_list = $作者 - >姓氏; // $ author_last_name_list。= $ author_last_name_list; $ author_last_name_list连接数组($ Author-> LastName); } }(这不起作用)但我如何在foreach循环中构建数组?第二,如果我有大名单,我如何得到一个新的阵列“名称在阵列中多少次”...问候! – Thijs 2010-12-17 09:50:19

+0

编辑完成后,这是一个有效的问题。 – 2010-12-17 11:15:33

回答

0

这里有一些递归数组搜索函数的例子:http://php.net/manual/en/function.array-search.php。只需修改它们即可将所有找到的实例作为数组返回,而不仅仅是第一个找到的匹配。祝你好运! :)

EDIT(不妨为它工作...):

我相信你可以得到所有Lastname小号SimpleXML的,但让我们跟你一起去有什么。我想你的意思是这样的:

$author_last_name_list = array(); 
foreach($xml->xpath('//AuthorList') as $AuthorList) { 
    foreach ($AuthorList->Author as $Author) { 
     // $author_last_name_list join array($Author->LastName); 
     if (array_key_exists($Author, $author_last_name_list) { 
      $author_last_name_list[$Author] += 1; 
     } else { 
      $author_last_name_list[$Author] = 1; 
     } 
    } 
} 
+0

嘿,我添加了一些代码,但现在我看到@Felix Kling已经做了一切。所以,祝你好运! :) – 2010-12-17 12:22:47

3

首先,如果my answer to your previous question帮助你,那么你应该接受的答案。
如果我的代码不起作用,那么你应该评论我的答案,并告诉我,以便我可以纠正我的答案。

其次,创建在一个循环中的阵列只是增加值设置为空数组:

$authors = array(); 
foreach ($xml->xpath('//AuthorList') as $AuthorList) { 
    foreach ($AuthorList->Author as $Author) { 
     $name = (string) $Author->LastName; 
     if(!array_key_exists($name, $authors)) { 
      $authors[$name] = 0; 
     } 
     $authors[$name] += 1; 
    } 
} 

后来遍历$authors。这些键包含名称和值的出现次数。

更新:(因为我很好这段时间;))

foreach($authors as $name=>$times) { 
    echo $name . "($times)" . "<br />"; // or PHP_EOL if you print it on a terminal 
} 

了解更多关于arrays in PHP


关于你的代码:即使它不工作,很短的一句话:

$author_last_name_list = $Author->LastName; 
$author_last_name_list .= $author_last_name_list; 

有了这个,你是第一次使用变量设置$author_last_name_list$Author->LastName,并覆盖在$author_last_name_list前值,然后你连接。你将永远得到的是:

$author_last_name_list = "A"; 
$author_last_name_list .= $author_last_name_list; // => 'AA'; 

如果你想获得的名字在一个字符串(不帮助你在这里是这样),那么你就必须初始化前的变量循环:

$author_last_name_list = ''; 
foreach($xml->xpath('//AuthorList') as $AuthorList) { 
    foreach ($AuthorList->Author as $Author) { 
     $author_last_name_list .= $Author->LastName; 
    } 
} 

而且

$author_last_name_list join array($Author->LastName); 

是无效的PHP语法。

+0

非常感谢您...... 1.我如何“接受您的回答”是否有一个sumbit按钮? (对不起这里很新)2.我可以看到代码中的逻辑!但它不起作用...我得到这个错误:Warning:array_key_exists()[function.array-key-exists]:第一个参数应该是/ var/www/phptest/get_lastnames中的字符串或整数。第24行的PHP ...所以它说变量中应该有一个字符串,我检查,并有一个字符串...? – Thijs 2010-12-17 10:50:08

+0

@ Thijs:1.答案左侧有一个勾号(就在投票柜台下方)。点击它接受答案。你也应该为你的其他问题做到这一点! (如果他们有一个工作答案)。 2.你得到错误是因为'$ name'不是一个字符串。它仍然是一个'SimpleXMLElement'。它只是在打印时转换为字符串。看到我更新的答案(你必须将其转换为字符串)。 – 2010-12-17 11:10:08

+0

我“回答”了前面的问题,感谢您的解释......我有这个几乎工作,现在当我回声,$作者我得到“数组”我如何显示此数组的内容?谢谢!!! – Thijs 2010-12-17 11:43:00