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

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6179659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:42:30+00:00 2026-05-24T00:42:30+00:00

According to the manual : Returns a SimpleXMLElement element, whether the node has children

  • 0

According to the manual:

Returns a SimpleXMLElement element, whether the node has children or not.

But first of all, this doesn’t make sense to me. children() should surely return an array of SimpleXMLElements. If the node has more than one child, how can returning a single SimpleXMLElement possibly represent that?

Second of all, that doesn’t tie in with the example that follows in the manual:

foreach ($xml->children() as $second_gen)
   echo ' The person begot a ' . $second_gen['role'];

How can you foreach through a SimpleXMLElement? I thought you could only do that with arrays? Also, what the heck’s going on with the $second_gen values? So children() returns an array of associative arrays…?

There must be some somewhat deep PHP grammar rules going on here that I’m not aware of so please explain or point me to the relevant manual pages.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T00:42:32+00:00Added an answer on May 24, 2026 at 12:42 am

    Why not try for yourself (demo):

    $foo = new SimpleXmlElement(
    '<foo>
        <bar>
            <baz id="b1"/>
            <baz id="b2"/>
            <baz id="b3"/>
        </bar>
    </foo>
    ');
    

    Then do

    var_dump($foo->bar->baz[0]->children());
    

    Output

    object(SimpleXMLElement)#4 (0) {}
    

    Then do

    var_dump($foo->bar->children());
    

    Output

    object(SimpleXMLElement)#2 (1) {
      ["baz"]=>
      array(3) {
        [0]=>
        object(SimpleXMLElement)#3 (0) {
        }
        [1]=>
        object(SimpleXMLElement)#5 (0) {
        }
        [2]=>
        object(SimpleXMLElement)#6 (0) {
        }
      }
    }
    

    How can you foreach through a SimpleXMLElement? I thought you could only do that with arrays?

    Well, that’s wrong. You can foreach any Traversable (e.g. objects and arrays, etc) and any object implementing the Iterator interface. Here is the class signature of SimpleXmlElement

    Class [ <internal:SimpleXML> <iterateable> class SimpleXMLElement implements Traversable ] {
    

    Also, what the heck’s going on with the $second_gen values? So children() returns an array of associative arrays…?

    No, it doesnt. It returns a SimpleXmlElement. Accessing a SimpleXmlElement by Array Access fetches attributes from the current node (or accesses a child node by position if numeric), e.g.

    echo $foo->bar->baz[2]['id']; // prints "b3"
    

    This is somewhat non-obvious because the SimpleXmlElement doesnt implement ArrayAccess but it’s how it is implemented in C. See http://www.php.net/manual/en/simplexml.examples-basic.php for more examples.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According to the manual and my code, this should be working perfect, but it
According to the PHP manual , a class like this: abstract class Example {}
According to this discussion , the iphone agreement says that it doesn't allow loading
According to HG manual : By default this command prints revision number and changeset
According to this manual I tried to set the proxy extension. In my_prof_dir/extensions there
The Emacs documentation has a section on character classes . According to the manual,
According to the manual of mysql_query() and to everything I know about this function
is_dir() according to the php manual returns true if passed a string that refers
According to this link: http://logback.qos.ch/manual/appenders.html (in RollingFileAppender) It seems to me that Logback provides
According to the manual , git dcommit will create a revision in SVN for

Explore

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

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

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.