Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow

StackOverflow Logo StackOverflow Logo

StackOverflow Navigation

Search
Ask A Question

Mobile menu

Close
Ask A Question
  • Home
  • Add group
  • Feed
  • User Profile
  • Communities
  • Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
Home/ Questions/Q 754
Next

StackOverflow Latest Questions

Saralyn
  • 0
  • 0
SaralynBegginer
Asked: May 1, 20252025-05-01T23:52:06+00:00 2025-05-01T23:52:06+00:00In: PHP

Get last value of a sub-node in XML parsed by PHP into an array

  • 0
  • 0
Get last value of a sub-node in XML parsed by PHP into an array

I am trying to parse an Atom XML feed using PHP. It is for an ebook publisher. All is well, except I cannot work out how to extract the last “subject” tag for each entry: that is the publisher’s tag (instead of Library of Congress subject heading = LCSH), and there are a variable number of those for each title.

Here’s some sample code, and what I’ve got so far:

Feed XML (slightly abbreviated; three book’s worth)




    https://standardebooks.org/feeds/atom/new-releases
    
    Standard Ebooks - Newest Ebooks
    The 15 latest Standard Ebooks, most-recently-released first.
    https://standardebooks.org/images/logo.png
    2025-04-29T17:50:47Z
    
        Standard Ebooks
        https://standardebooks.org
    
    
    
        https://standardebooks.org/ebooks/anthony-berkeley/the-poisoned-chocolates-case
        The Poisoned Chocolates Case
        
            Anthony Berkeley
            https://standardebooks.org/ebooks/anthony-berkeley
        
        2025-04-21T03:39:39Z
        2025-04-21T09:01:35Z
        Public domain in the United States. Users located outside of the United States must check their local laws before using this ebook. Original content released to the public domain via the Creative Commons CC0 1.0 Universal Public Domain Dedication.
        A club of amateur sleuths attempts to solve a crime whose solution has eluded Scotland Yard.
        
        
        
        
        
        
    
    
        https://standardebooks.org/ebooks/g-k-chesterton/whats-wrong-with-the-world
        What’s Wrong with the World
        
            G. K. Chesterton
            https://standardebooks.org/ebooks/g-k-chesterton
        
        2025-04-29T03:11:02Z
        2025-04-29T03:11:05Z
        Public domain in the United States. Users located outside of the United States must check their local laws before using this ebook. Original content released to the public domain via the Creative Commons CC0 1.0 Universal Public Domain Dedication.
        Philosopher G. K. Chesterton comments on various social issues of his day, including property, imperialism, feminism, and education.
        
        
        
    
    
        https://standardebooks.org/ebooks/george-gissing/the-private-papers-of-henry-ryecroft
        The Private Papers of Henry Ryecroft
        
            George Gissing
            https://standardebooks.org/ebooks/george-gissing
        
        2025-04-29T17:21:20Z
        2025-04-29T17:49:20Z
        Public domain in the United States. Users located outside of the United States must check their local laws before using this ebook. Original content released to the public domain via the Creative Commons CC0 1.0 Universal Public Domain Dedication.
        A retired Englishman muses on life and literature in his private diary.
        
        
    

PHP code

". PHP_EOL;
$url="atom-sample.xml";

$xmla = simplexml_load_file($url);
  $xml = array();
  foreach($xmla->entry as $node) {
    $xml[] = $node;
  }

foreach($xml as $entry){
  $entry->registerXPathNamespace('ns','http://www.w3.org/2005/Atom');
  echo '
  • '.$entry->author->name .', '. $entry->title .' | '. $entry->category['term'] .'
  • '; } echo ""; ?>

    Parsed array (output of print_r($xml))

    Array
    (
        [0] => SimpleXMLElement Object
            (
                [id] => https://standardebooks.org/ebooks/anthony-berkeley/the-poisoned-chocolates-case
                Get last value of a sub-node in XML parsed by PHP into an array => The Poisoned Chocolates Case
                 => SimpleXMLElement Object
                    (
                        [name] => Anthony Berkeley
                        [uri] => https://standardebooks.org/ebooks/anthony-berkeley
                    )
    
                [published] => 2025-04-21T03:39:39Z
                [updated] => 2025-04-21T09:01:35Z
                [rights] => Public domain in the United States. Users located outside of the United States must check their local laws before using this ebook. Original content released to the public domain via the Creative Commons CC0 1.0 Universal Public Domain Dedication.
                [summary] => A club of amateur sleuths attempts to solve a crime whose solution has eluded Scotland Yard.
                [category] => Array
                    (
                        [0] => SimpleXMLElement Object
                            (
                                [@attributes] => Array
                                    (
                                        [scheme] => http://purl.org/dc/terms/LCSH
                                        [term] => Detective and mystery stories
                                    )
    
                            )
    
                        [1] => SimpleXMLElement Object
                            (
                                [@attributes] => Array
                                    (
                                        [scheme] => http://purl.org/dc/terms/LCSH
                                        [term] => London (England) -- Fiction
                                    )
    
                            )
    
                        [2] => SimpleXMLElement Object
                            (
                                [@attributes] => Array
                                    (
                                        [scheme] => http://purl.org/dc/terms/LCSH
                                        [term] => Sheringham, Roger (Fictitious character) -- Fiction
                                    )
    
                            )
    
                        [3] => SimpleXMLElement Object
                            (
                                [@attributes] => Array
                                    (
                                        [scheme] => http://purl.org/dc/terms/LCSH
                                        [term] => Police -- England -- London -- Fiction
                                    )
    
                            )
    
                        [4] => SimpleXMLElement Object
                            (
                                [@attributes] => Array
                                    (
                                        [scheme] => https://standardebooks.org/vocab/subjects
                                        [term] => Fiction
                                    )
    
                            )
    
                        [5] => SimpleXMLElement Object
                            (
                                [@attributes] => Array
                                    (
                                        [scheme] => https://standardebooks.org/vocab/subjects
                                        [term] => Mystery
                                    )
    
                            )
    
                    )
    
            )
    
        [1] => SimpleXMLElement Object
            (
                [id] => https://standardebooks.org/ebooks/g-k-chesterton/whats-wrong-with-the-world
                Get last value of a sub-node in XML parsed by PHP into an array => What’s Wrong with the World
                 => SimpleXMLElement Object
                    (
                        [name] => G. K. Chesterton
                        [uri] => https://standardebooks.org/ebooks/g-k-chesterton
                    )
    
                [published] => 2025-04-29T03:11:02Z
                [updated] => 2025-04-29T03:11:05Z
                [rights] => Public domain in the United States. Users located outside of the United States must check their local laws before using this ebook. Original content released to the public domain via the Creative Commons CC0 1.0 Universal Public Domain Dedication.
                [summary] => Philosopher G. K. Chesterton comments on various social issues of his day, including property, imperialism, feminism, and education.
                [category] => Array
                    (
                        [0] => SimpleXMLElement Object
                            (
                                [@attributes] => Array
                                    (
                                        [scheme] => http://purl.org/dc/terms/LCSH
                                        [term] => Social problems
                                    )
    
                            )
    
                        [1] => SimpleXMLElement Object
                            (
                                [@attributes] => Array
                                    (
                                        [scheme] => https://standardebooks.org/vocab/subjects
                                        [term] => Nonfiction
                                    )
    
                            )
    
                        [2] => SimpleXMLElement Object
                            (
                                [@attributes] => Array
                                    (
                                        [scheme] => https://standardebooks.org/vocab/subjects
                                        [term] => Philosophy
                                    )
    
                            )
    
                    )
    
            )
    
        [2] => SimpleXMLElement Object
            (
                [id] => https://standardebooks.org/ebooks/george-gissing/the-private-papers-of-henry-ryecroft
                Get last value of a sub-node in XML parsed by PHP into an array => The Private Papers of Henry Ryecroft
                 => SimpleXMLElement Object
                    (
                        [name] => George Gissing
                        [uri] => https://standardebooks.org/ebooks/george-gissing
                    )
    
                [published] => 2025-04-29T17:21:20Z
                [updated] => 2025-04-29T17:49:20Z
                [rights] => Public domain in the United States. Users located outside of the United States must check their local laws before using this ebook. Original content released to the public domain via the Creative Commons CC0 1.0 Universal Public Domain Dedication.
                [summary] => A retired Englishman muses on life and literature in his private diary.
                [category] => Array
                    (
                        [0] => SimpleXMLElement Object
                            (
                                [@attributes] => Array
                                    (
                                        [scheme] => http://purl.org/dc/terms/LCSH
                                        [term] => English fiction -- 19th century
                                    )
    
                            )
    
                        [1] => SimpleXMLElement Object
                            (
                                [@attributes] => Array
                                    (
                                        [scheme] => https://standardebooks.org/vocab/subjects
                                        [term] => Fiction
                                    )
    
                            )
    
                    )
    
            )
    
    )
    

    Output

    1. Anthony Berkeley, The Poisoned Chocolates Case | Detective and mystery stories
    2. G. K. Chesterton, What’s Wrong with the World | Social problems
    3. George Gissing, The Private Papers of Henry Ryecroft | English fiction -- 19th century

    The value I’m after is the last one under [category], which is [5] for the first book, [2] for the second book, and [1] for the third book. Above, I’ve used $entry->category['term'] for this “slot”, which always returns the [0] value—but I want the last one, which is not consistent!

    I am aware of the [last()] construct, but no permutation of my code using [last()] has worked. Obviously I’m doing something wrong in handling that sub-array with my approach/code. So what works?! Thanks!

    0
    • 0 0 Answers
    • 34 Views
    • 0 Followers
    • 0
    Answer
    Share
    • Facebook
    • Report

    Leave an answer
    Cancel reply

    You must login to add an answer.

    Forgot Password?

    Need An Account, Sign Up Here

    Sidebar

    Ask A Question
    • Popular
    • Answers
    • W3spoint99

      What is Physics? Definition, History, Importance, Scope (Class 11)

      • 1 Answer
    • W3spoint99

      The Living World – Introduction, Classification, Characteristics, FAQs (Class 11 ...

      • 1 Answer
    • W3spoint99

      Explain - Biological Classification (Class 11 - Biology)

      • 1 Answer
    • Saralyn
      Saralyn added an answer When Humans look at their childhood pictures, the first thing… January 17, 2025 at 3:25 pm
    • Saralyn
      Saralyn added an answer Previously, length was measured using units such as the length… January 17, 2025 at 3:25 pm
    • Saralyn
      Saralyn added an answer Measurement forms the fundamental principle to various other branches of… January 17, 2025 at 3:25 pm

    Related Questions

    • Reading fancy apostrophe PHP [duplicate]

      • 0 Answers
    • Unable to send mail via PHPMailer [SMTP->Error: Password not accepted ...

      • 0 Answers
    • Concerns when migrating from PHP 5.6 to 8.4 [closed]

      • 0 Answers
    • Laravel Auth::attempt() error: "Unknown column 'password'" when using a custom ...

      • 0 Answers
    • Core PHP cURL - header origin pass null value

      • 0 Answers

    Trending Tags

    biology class 11 forces how physics relates to other sciences interdisciplinary science learn mathematics math sets tutorial null sets physics physics and astronomy physics and biology physics and chemistry physics applications science science connections science education sets in mathematics set theory basics types of sets types of sets explained

    Explore

    • Home
    • Add group
    • Groups page
    • Communities
    • Questions
      • New Questions
      • Trending Questions
      • Must read Questions
      • Hot Questions
    • Polls
    • Tags
    • Badges
    • Users
    • Help

    Footer

    • About US
    • Privacy Policy
    • Questions
    • Recent Questions
    • Web Stories

    © 2025 WikiQuora.Com. All Rights Reserved

    Insert/edit link

    Enter the destination URL

    Or link to existing content

      No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.