Stu
Stu

Reputation: 4150

Detecting simpleXml array property

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?

Upvotes: 7

Views: 1619

Answers (4)

Bencho Naut
Bencho Naut

Reputation: 348

( since this XML thingy caused tons of trouble for me , i switched to the "give me an associative array" by using

$myarray = json_decode(json_encode(simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA)), true);

That way the "CDATA[" parts are also included as clear text

regards

Upvotes: 0

Tama
Tama

Reputation: 311

I think that the main point was indicated by Yegor Lukash, saying that it's an Iterator.

However, you can get the expected result this way:

foreach($xml->category as $node)
{
    $subcategories = ((array)$node)['subcategory'];   
    var_dump($subcategories);
}

About the feskr answer, I think it can be useful to obtain a clearer xml code, and in such case my solution would become:

foreach($xml->category as $node)
{
    $subcategories = ((array)$node->subcategories)['subcategory'];
    var_dump($subcategories);
}

But in that case it would be better this:

foreach($xml->category as $node)
{
    foreach($node->subcategories->children() as $subcategory) {
      var_dump($subcategory);
    }
}

Upvotes: 0

Yegor Lukash
Yegor Lukash

Reputation: 510

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)

Upvotes: 3

feskr
feskr

Reputation: 829

Adjust your xml file:

<result>
    <category>
        <id>3</id>
        <name>Category 1</name>
        <subcategories>
          <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>
        </subcategories>
    </category>
</result>

And try:

var_dump($node->subcategories);

Upvotes: 1

Related Questions