【问题标题】:PHP get values from SimpleXMLElement arrayPHP 从 SimpleXMLElement 数组中获取值
【发布时间】:2011-02-14 15:42:14
【问题描述】:

我有这个:

  [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"
  }
}

(我使用 var_dump($child) 来生成)

如何将“名称”属性作为字符串取出?

这是我的代码:

$xml = simplexml_load_string($results);

foreach($xml->data->list as $child) {
var_dump($child);
  echo $child->getName() . ": " . $child->name . "<br />";
  }

【问题讨论】:

    标签: php arrays string simplexml


    【解决方案1】:

    使用 SimpleXML,您可以获得:

    • 子元素,使用对象表示法:$element-&gt;subElement
    • 和属性,使用数组表示法:$element['attribute']


    所以,在这里,我想说你必须使用:

    echo $child['name'];
    


    作为参考和几个示例,请参阅 simplexml 手册的 Basic usage 部分。

    示例 #6 应该是有趣的一个,关于属性。

    【讨论】:

    • 我遇到了这个确切的问题,我必须在所有属性前添加(string) 才能将它们作为字符串获取。很混乱。 (谢谢,PHP!) 编辑:我刚刚意识到 JW。下面说了同样的话。无论如何,希望这对那些急于回答标记为正确并且不查看任何其他答案的人有所帮助。
    【解决方案2】:

    有点乱,但我成功使用了这个

    foreach($xml->data->children() as $child) {
    //var_dump($child);
        foreach ($child->attributes() as $a => $b) {
         echo $a . '=' . $b . '<br />';
        }
    }
    

    不知道为什么,但是 OpsView API 返回一个二维数组,而不是每个 XML 节点只有一个值:(

    echo $child['name'];
    

    工作并且更加优雅,谢谢。

    【讨论】:

      【解决方案3】:

      虽然你可以这样做:

      echo $child['name'];
      

      要查看值,您应该注意$child['name'] 是一个对象,而不是一个字符串。回显它会将其转换为字符串,因此它适用于这种情况。但如果你将它存储在某个地方,最好自己将其转换为字符串:

      $name = (string) $child['name'];
      

      【讨论】:

        【解决方案4】:

        我遇到了类似的问题,我需要从 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)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-02-18
          • 1970-01-01
          • 2011-02-21
          • 2013-02-12
          • 1970-01-01
          • 2021-01-03
          • 2015-11-28
          • 2018-05-05
          相关资源
          最近更新 更多