php 数组子集

发布于 2024-10-08 09:58:25 字数 27808 浏览 5 评论 0原文

我需要从这个 simplexmlobject:

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. [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 循环中构建数组?第二,如果我有大名单,我如何获得一个新数组,其中包含“该名称在数组中出现了多少次”...问候!

I need to go from this simplexmlobject:

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. [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

To an array that looks like:

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)

Regards!

First thing i need to do is get all the Last_names in one array, this should be done with a foreach loop:

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); 
    } 
} 

(this doesn't work)

but how do i build an array in a foreach loop? 2nd if i have the big name list, how do i get a new array with "how many times the name was in the array"... Regards!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

相思故 2024-10-15 09:58:25

首先,如果我对您上一个问题的回答有帮助你,那么你应该接受答案。
如果我的代码不起作用,那么您应该评论我的答案并告诉我,以便我可以更正我的答案。

其次,在循环中创建数组只是将值添加到空数组中:

$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
}

阅读有关 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 语法。

First, if my answer to your previous question helped you, then you should accept the answer.
If my code does not work then you should comment my answer and tell me that so that I can correct my answer.

Second, creating an array in a loop is just adding values to an empty array:

$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;
    }
}

And later loop over $authors. The keys contain the name and the value the occurrences.

update: (because I'm nice this time ;))

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

Read more about arrays in PHP.


Regarding your code: Even if it does not work, a short remark:

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

With this, you are first setting the variable $author_last_name_list to $Author->LastName, overriding the value that was in $author_last_name_list before, and then you concatenate. What you will always get is:

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

If you want to get the names in a string (which does not help you here anyway) then you would have to initialize the variable before the loop:

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

And

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

is not valid PHP syntax.

尽揽少女心 2024-10-15 09:58:25

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

编辑(也许还可以...):

我相信您可以获取所有 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;
        }
    } 
} 

There are some examples of recursive array search functions here: http://php.net/manual/en/function.array-search.php. Just alter them to return all found instances as an array instead of only the first found match. Good luck! :)

EDIT (might as well work for it...):

I believe you can just get all Lastnames SimpleXML, but let's go with what you have. I think you mean something like this:

$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;
        }
    } 
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文