0

I am pretty new to PHP an XML and hope you can help me with this. Searching the forum didn't help me yet to find an answer to my specific issue.

I have a PHP page with a simplexml array that looks like the following, just longer:

    SimpleXMLElement Object 
    ( 
       [textID] => Array 
                ( 
                   [0] => SimpleXMLElement Object 
                       ( 
                          [textID] => 1 
                          [content] => Text1 
                       ) 
                   [1] => SimpleXMLElement Object 
                       ( 
                          [textID] => 2 
                          [content] => Text2
                       ) 
                   [2] => SimpleXMLElement Object 
                       ( 
                          [textID] => 3 
                          [content] => Text3 
                       ) 
                )
      )

Now I am trying to echo out a specific value from this array by referring to its ID which is an integer. The only way I get this working is the following but this just goes by the order within the array, not by the actual ID:

    <?php echo $objTexts->textID[1]->content; ?>

Can someone tell me what I am missing here ?

Thanks, Tim

12
  • Can u show a piece of your xml? Commented Aug 4, 2013 at 11:42
  • I did, it's at the beginning of the code. Commented Aug 4, 2013 at 11:45
  • I copied my XMl 1:1 and just shortened it. Commented Aug 4, 2013 at 11:46
  • Hmm? it's looks strange) It must looks somthing like this: <?xml version='1.0'?> <objTexts> <textID> <content> Some text </content> </textID> </objTexts> Commented Aug 4, 2013 at 11:50
  • That's how it looks when I use print_r on the page. I am creating it through a stored procedure in SQL Server. Commented Aug 4, 2013 at 11:53

1 Answer 1

1

SimpleXML has no way of knowing that the textID identifies which node is which - it is just another element in the XML.

Based on your sample output, your XML is a little confusing as you have multiple elements called textID which each have a single child, also called textID, which has a different meaning. Nonetheless, what you want to do can be achieved either by looping through all the outer textID elements and testing the value of their inner textID element:

foreach ( $objTexts->textID as $item )
{
     if ( $item->textID == '2' )
     {
          ...
     }
}

Or, you could use XPath, which is a fairly simple query language for XML, and is supported within SimpleXML in the form of the ->xpath() method. In your case, you want to find a textID node which contains a textID child with a particular value, so the code would look something like this:

 // ->xpath always returns a plain PHP array - not a SimpleXML object
 $xpath_results = $objTexts->xpath('//textID[textID=2]');
 
 // If you're certain you only want the first result:
 echo $xpath_results[0]->content;
 
 // If you might want multiple matches
 foreach ( $xpath_results as $item )
 {
     ...
 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.