0

我正在尝试解析以下响应:

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 ) 
) ) ) 

在 PHP 中。我将它存储为 curl 字符串:

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

如何将第一个对象的 conceptNui 作为 PHP 变量获取?

4

2 回答 2

1

看一下文档,它给出了简单的例子来理解提取数据的友好语法:

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

在你的情况下,它可能是:

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

因为你的结构是

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 ) 
              )
          )
  )
于 2013-03-20T05:11:22.033 回答
1
   $conceptNui = $mainObj->groupConcepts->concept[0]->conceptNui;
于 2013-03-20T05:16:26.117 回答