PHP 从 SimpleXMLElement 数组中获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/2751711/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP get values from SimpleXMLElement array
提问by seanp2k
I have this:
我有这个:
  [1]=>
object(SimpleXMLElement)#6 (1) {
  ["@attributes"]=>
  array(14) {
    ["name"]=>
    string(5) "MySQL"
    ["acknowledged"]=>
    string(1) "1"
    ["comments"]=>
    string(1) "1"
    ["current_check_attempt"]=>
    string(1) "1"
    ["downtime"]=>
    string(1) "0"
    ["last_check"]=>
    string(19) "2010-05-01 17:57:00"
    ["markdown_filter"]=>
    string(1) "0"
    ["max_check_attempts"]=>
    string(1) "3"
    ["output"]=>
    string(42) "CRITICAL - Socket timeout after 10 seconds"
    ["perfdata_available"]=>
    string(1) "1"
    ["service_object_id"]=>
    string(3) "580"
    ["state"]=>
    string(8) "critical"
    ["state_duration"]=>
    string(6) "759439"
    ["unhandled"]=>
    string(1) "0"
  }
}
(I used var_dump($child) to generate that)
(我使用 var_dump($child) 来生成)
How do I get the 'name' attribute out of there as a string?
如何将 'name' 属性作为字符串取出?
Here is my code:
这是我的代码:
$xml = simplexml_load_string($results);
foreach($xml->data->list as $child) {
var_dump($child);
  echo $child->getName() . ": " . $child->name . "<br />";
  }
回答by JW.
While you can do:
虽然你可以这样做:
echo $child['name'];
to see the value, you should note that $child['name']is an object, not a string. Echoing it casts it to a string, so it works in that situation. But if you're storing it somewhere, it's better to cast it to a string yourself:
要查看该值,您应该注意它$child['name']是一个对象,而不是一个字符串。回显它会将其转换为字符串,因此它可以在这种情况下工作。但是如果你将它存储在某个地方,最好自己将它转换为字符串:
$name = (string) $child['name'];
回答by Pascal MARTIN
With SimpleXML, you can get :
使用 SimpleXML,您可以获得:
- sub-elements, using object notation : 
$element->subElement - and attributes, using array notation : 
$element['attribute'] 
- 子元素,使用对象表示法: 
$element->subElement - 和属性,使用数组表示法: 
$element['attribute'] 
So, here, I'd say you'd have to use :
所以,在这里,我想说你必须使用:
echo $child['name'];
As a reference, and for a couple of examples, see the Basic usagesection of simplexml's manual.
作为参考和一些示例,请参阅simplexml 手册的基本用法部分。
Example #6should be the interesting one, about attributes.
Example #6应该是有趣的一个,关于属性。
回答by seanp2k
Kind of messy, but I used this successfully
有点乱,但我成功地使用了这个
foreach($xml->data->children() as $child) {
//var_dump($child);
    foreach ($child->attributes() as $a => $b) {
     echo $a . '=' . $b . '<br />';
    }
}
Not sure why, but the OpsView API returns a two-dimensional array instead of just having one value per XML node :(
不知道为什么,但 OpsView API 返回一个二维数组,而不是每个 XML 节点只有一个值:(
echo $child['name'];
works and is much more elegant, thank you.
有效并且更优雅,谢谢。
回答by CE_REAL
I had a similar problem, I needed to get the string out of my SimpleXMLElement, I couldn't find the name to call it. Found the solution, by using (string) to get the string text:
我遇到了类似的问题,我需要从 SimpleXMLElement 中取出字符串,但找不到名称来调用它。找到解决方案,通过使用 (string) 获取字符串文本:
foreach ($lines as $line) {
    array_push($result, new line(**(string)**$line));
}
array
  0 => 
    object(line)[190]
      private '_line' => 
        object(SimpleXMLElement)[128]
          public '@attributes' => 
            array
              ...
          string ' ' (length=1)
  1 => 
    object(line)[191]
      private '_line' => 
        object(SimpleXMLElement)[131]
          public '@attributes' => 
            array
              ...
          string ' ' (length=1)
  2 => 
    object(line)[192]
      private '_line' => 
        object(SimpleXMLElement)[132]
          public '@attributes' => 
            array
              ...
          string ' ~54**** I N V O I C E ****' (length=27)

