simplexml: how to get NUMERIC array key

They have: 2 posts

Joined: Jan 2010

Hello,

I am using simplexml and cannot find what I think would be a simple solution.

How do I get the numeric array key for each <item>?

Please see the simple example below....

My xml:

<data>
  <artist>Chris Navarro</artist>
  <type>Sculpture</type>
  <gallery>
    <name>General</name>
    <item>
       <title>A Mother's Love</title>
       <img>a_mothers_love.jpg</img>
    </item>
    <item>
      <title>Duality of the Bull and Bear</title>
      <img>duality_of_the_bull_and_bear.jpg</img>
    </item>
    <item>
      <title>Effort And Desire</title>
      <img>effort_and_desire.jpg</img>
    </item>
  </gallery>
</data>

Then printing out my simplexml object, with:

print_r($xml->gallery[0]->children());

Which outputs:

SimpleXMLElement Object
(
    [name] => General
    [item] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [title] => A Mother's Love
                    [img] => a_mothers_love.jpg
                )
            [1] => SimpleXMLElement Object
                (
                    [title] => Duality of the Bull and Bear
                    [img] => duality_of_the_bull_and_bear.jpg
                        )
                )
            [2] => SimpleXMLElement Object
                (
                    [title] => Effort And Desire
                    [img] => effort_and_desire.jpg
                )
        )
)

Then when I loop through with:

foreach($xml->gallery[0]->item as $key=>$item){
  echo $key.': '.$item.'<br />';
}

Outputs the following:
item: A Mother's Love
item: Duality of the Bull and Bear
item: Effort And Desire

But what I'm looking for is the numeric array keys, like:
0: A Mother's Love
1: Duality of the Bull and Bear
2: Effort And Desire

Please help, thanks in advance.

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

Long story short, I found that this works:

$items = array();
foreach($xml->gallery[0]->item as $key=>$item)
{
     $items[] = (string)$item->title;
}

var_dump($items);

But ya, dealing with a nest of objects and arrays are annoying, which is why I particularly find dealing with SimpleXMLElement frustrating at times.

Perfect example, when I take your example and var_dump($xml->gallery[0]); I get:

object(SimpleXMLElement)#8 (2) {
  ["name"]=>
  string(7) "General"
  ["item"]=>
  array(3) {
    [0]=>
    object(SimpleXMLElement)#6 (2) {
      ["title"]=>
      string(15) "A Mother's Love"
      ["img"]=>
      string(18) "a_mothers_love.jpg"
    }
    [1]=>
    object(SimpleXMLElement)#7 (2) {
      ["title"]=>
      string(28) "Duality of the Bull and Bear"
      ["img"]=>
      string(32) "duality_of_the_bull_and_bear.jpg"
    }
    [2]=>
    object(SimpleXMLElement)#9 (2) {
      ["title"]=>
      string(17) "Effort And Desire"
      ["img"]=>
      string(21) "effort_and_desire.jpg"
    }
  }
}

So, one would think that if I var_dump($xml->gallery[0]->item); I would get the item array, but I get this:

object(SimpleXMLElement)#6 (2) {
  ["title"]=>
  string(15) "A Mother's Love"
  ["img"]=>
  string(18) "a_mothers_love.jpg"
}

So based on that, I figured that looping through the object and build the array manually works the best.

They have: 2 posts

Joined: Jan 2010

Exactly! So frustrating. Seems odd that I can pass a key like this:

$xml->gallery[0]->item[1];

...and it will give me that exact object from the [item][1] array of objects, but there is no way to pull that key?

Thanks for the reply pr0gr4mm3r!

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.