Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 568
Stu Avatar asked Oct 03 '12 14:10

Stu


2 Answers

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)
like image 132
Yegor Lukash Avatar answered Sep 30 '22 15:09

Yegor Lukash


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);
like image 31
feskr Avatar answered Sep 30 '22 15:09

feskr