PHP 从 SimpleXMLElement 数组获取值

发布于 2024-08-30 20:47:09 字数 1101 浏览 1 评论 0原文

我有这个:(

  [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) 来生成它)

如何以字符串形式获取“name”属性?

这是我的代码:

$xml = simplexml_load_string($results);

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

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)

How do I get the 'name' attribute out of there as a string?

Here is my code:

$xml = simplexml_load_string($results);

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

指尖上的星空 2024-09-06 20:47:10

使用 SimpleXML,您可以获得:

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

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

echo $child['name'];

作为参考和一些示例,请参阅 Basic simplexml 手册的用法部分。

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

With SimpleXML, you can get :

  • sub-elements, using object notation : $element->subElement
  • and attributes, using array notation : $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 usage section of simplexml's manual.

Example #6 should be the interesting one, about attributes.

新人笑 2024-09-06 20:47:10

虽然您可以执行以下操作:

echo $child['name'];

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

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

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:

$name = (string) $child['name'];
我爱人 2024-09-06 20:47:10

有点混乱,但我成功地使用了它

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'];

有效并且更加优雅,谢谢。

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 :(

echo $child['name'];

works and is much more elegant, thank you.

等待我真够勒 2024-09-06 20:47:10

当我第一次遇到这个问题时,这真的很令人困惑。在尝试获取整个数组时,我似乎返回了数组中的第一个对象!
在我的实例中,“item”是一个对象数组,以下仅返回数组中的第一个!

$response->channel->item

但是,如果像我一样,您想要访问所有对象,您只需循环项目即可,所以这并不是什么问题。真的很奇怪!

foreach($response->channel->item as $item) {
    ray($item);
}

This was really confusing when i first encountered this. While trying to get the whole array, instead I was seemingly returned the first object in the array instead!
In my instance "item" was an array of objects and the following only returned the first in the array!

$response->channel->item

However if like me, you want to access all the objects you can just loop over item, so not really a problem. Just really strange!

foreach($response->channel->item as $item) {
    ray($item);
}
灼疼热情 2024-09-06 20:47:10

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

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:

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文