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 8628025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:25:49+00:00 2026-06-12T08:25:49+00:00

I think I’m missing something really obvious here, but can someone explain to me

  • 0

I think I’m missing something really obvious here, but can someone explain to me why I’m getting the output I am and not the output I expect on the following var dumps:

Here’s the original xml:

<result>
    <category>
        <id>3</id>
        <name>Category 1</name>
        <subcategory>
            <id>9</id>
            <name>SubCat 1</name>
        </subcategory>
        <subcategory>
            <id>10</id>
            <name>SubCat 2</name>
        </subcategory>
        <subcategory>
            <id>11</id>
            <name>SubCat 3</name>
        </subcategory>
    </category>
</result>

What I am doing:

$xml = new SimpleXMLElement($file);
foreach($xml->category as $node)
{
    echo "dump 1:
";
    var_dump($node);
    echo "**********************************************
dump 2:
";
    var_dump($node->subcategory);
    die();
}

This outputs:

dump 1:
object(SimpleXMLElement)#130 (3) {
    ["id"]=>
    string(1) "3"
    ["name"]=>
    string(10) "Category 1"
    ["subcategory"]=>
    array(3) {
        [0]=>
        object(SimpleXMLElement)#133 (2) {
            ["id"]=>
            string(1) "9"
            ["name"]=>
            string(8) "SubCat 1"
        }
        [1]=>
        object(SimpleXMLElement)#135 (2) {
            ["id"]=>
            string(2) "10"
            ["name"]=>
            string(8) "SubCat 2"
        }
        [2]=>
        object(SimpleXMLElement)#136 (2) {
            ["id"]=>
            string(2) "11"
            ["name"]=>
            string(8) "SubCat 3"
        }
    }
}
**********************************************
dump 2:
object(SimpleXMLElement)#138 (2) {
    ["id"]=>
    string(1) "9"
    ["name"]=>
    string(8) "SubCat 1"
}

The first var dump outputs what I’d expect, but the output I would expect for the second var_dump would be:

array(3) {
    [0]=>
    object(SimpleXMLElement)#133 (2) {
        ["id"]=>
        string(1) "9"
        ["name"]=>
        string(8) "SubCat 1"
    }
    [1]=>
    object(SimpleXMLElement)#135 (2) {
        ["id"]=>
        string(2) "10"
        ["name"]=>
        string(8) "SubCat 2"
    }
    [2]=>
    object(SimpleXMLElement)#136 (2) {
        ["id"]=>
        string(2) "11"
        ["name"]=>
        string(8) "SubCat 3"
    }
}

Or even an object containing all the array items. Why is this not the case?

I can see when I call var_dump($node->subcategory) it’s dumping the first ‘subcategory’ node that it finds, but why then does it cast all the ‘subcategory’ nodes to an array for the first var dump but not for the second? And how would I mimic this behaviour to detect if ‘subcategory’ contains more than one object (as it does in the first var dump)?

Basically what I’m trying to do is detect if a property of the SimpleXMLElement contains an array of more values (i.e. if it contains child nodes)

I’ve tried all sorts, but I can’t seem to detect if one of the properties of the simpleXml object contains a set of arrays.

Update:

I found this works:

if(count($node->subcategory)>1)
{
    // we have  more than one subcategory
}

But it’s not the most elegant way, I’m sure there must be a cleaner method?

  • 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-06-12T08:25:50+00:00Added an answer on June 12, 2026 at 8:25 am

    This property is object of iterarator:

    $node->subcategory
    

    And if you need to get all items try:

    foreach($xml->category as $node)
    {
    ...
      foreach($node->subcategory as $subcategory)
      {
         $data[] = (array) $subcategory
      }
    ...
    }
    

    If you need to know count of subcategory items you successfully used count-method.

    And you may use xpath if will not change XML tree:

    $categories = $xml->xpath('/category/subcategory');
    var_dump($categories)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think I'm missing something basic here. Why is the third IF condition true?
I think I got all the steps down but I must be missing something
I think I have a basic understanding of this, but am hoping that someone
I think this has probably been asked, but after reading a lot, I'm not
I think I have a fairly basic question here. I'm not trying to waste
i think this should be very easy , but i really don't know how
I think I've seen this answer, but I can't remember where for certain. I'm
I think they are same thing but my boss say that is not right.
I think this could be a very easy question for you. But I have
Think Windows Explorer's 'Details' view. I need to output my database to this view.

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.