NCoder
NCoder

Reputation: 335

SimpleXMLElement parsing issue with PHP

I'm trying to parse out the following response:

SimpleXMLElement Object ( 
    [responseType] => SimpleXMLElement Object ( 
        [inputConceptName] => aspirin [inputKindName] => % ) 
    [groupConcepts] => SimpleXMLElement Object ( 
        [concept] => Array ( 
            [0] => SimpleXMLElement Object ( 
                [conceptName] => ASPIRIN 
                [conceptNui] => N0000145918 
                [conceptKind] => DRUG_KIND ) 
            [1] => SimpleXMLElement Object ( #
                [conceptName] => Aspirin 
                [conceptNui] => N0000006582 
                [conceptKind] => INGREDIENT_KIND ) 
) ) ) 

in PHP. I have it stored as a curl string:

$xml = simplexml_load_string($data);
print_r($xml);

How do I get just the conceptNui of the first object as a PHP variable?

Upvotes: 0

Views: 79

Answers (2)

bhab
bhab

Reputation: 169

   $conceptNui = $mainObj->groupConcepts->concept[0]->conceptNui;

Upvotes: 1

MatRt
MatRt

Reputation: 3534

Take a look at the documentation, it give simple example to understand the friendly syntax to extract the data :

http://www.php.net/manual/en/simplexml.examples-basic.php

In your case, it could be :

$xml->groupConcepts->concept[0]->conceptNui

As your structure is

SimpleXMLElement Object ( 
  [responseType] => SimpleXMLElement Object ( 
    [inputConceptName] => aspirin 
    [inputKindName] => % ) 
  [groupConcepts] => SimpleXMLElement Object ( 
    [concept] => Array ([0] => SimpleXMLElement Object ( 
                    [conceptName] => ASPIRIN 
                    [conceptNui] => N0000145918 
                    [conceptKind] => DRUG_KIND ) 
              [1] => SimpleXMLElement Object ( 
                    [conceptName] => Aspirin 
                    [conceptNui] => N0000006582 
                    [conceptKind] => INGREDIENT_KIND ) 
              )
          )
  )

Upvotes: 1

Related Questions