php - 如何使用简单的 xml 对象获取 xml 中的标签名称

标签 php

嗨,我有以下简单的 xml 对象:

[ad] => Array
    (
        [0] => SimpleXMLElement Object
            (
                [title] => <b>Title: Mysql 1</b>
                [abstract] => Abstract: Mysql 1
                [url] => www.Mysql.com
                [rurl] => http://sample/xtrack.php?rurldata=yes
                [iurl] => http://sample.com/
                [bid] => 0.0880
            )

        [1] => SimpleXMLElement Object
            (
                [title] => <b>Title: Mysql 2</b>
                [abstract] => Abstract: Mysql 2
                [url] => www.Mysql.com
                [rurl] => http://sample/xtrack.php?rurldata=yes
                [iurl] => http://sample.com/
                [bid] => 0.1980
            )
    )

我想从中创建一个数组。我正在尝试这样的 foreach

foreach($xmlString->ad as $key){
        foreach($key as $key2 => $val2){
             $adData[$counter][$key2] = $val2;
        }
            $counter++;
}

但是我得到了这样的结构:

 Array
  (
    [0] => Array
    (
        [title] => SimpleXMLElement Object
            (
                [0] => <b>Title: Mysql 1</b>
            )

        [abstract] => SimpleXMLElement Object
            (
                [0] => Abstract: Mysql 1
            )

        [url] => SimpleXMLElement Object
            (
                [0] => www.Mysql.com
            )

        [rurl] => SimpleXMLElement Object
            (
                [0] => http://sample/xtrack.php?rurldata=yes
            )

        [iurl] => SimpleXMLElement Object
            (
                [0] => http://sample.com/
            )

        [bid] => SimpleXMLElement Object
            (
                [0] => 0.0880
            )

    )
    .
    .
    .So on

现在我知道 $key2 是一个对象。我怎样才能只检索它的标签名称?这该怎么办?

最佳答案

您可以使用 SimpleXmlEmement::getName 获取名称方法

foreach($xmlString->ad as $key){
   echo $key->getName() . '<br/>';
}

关于php - 如何使用简单的 xml 对象获取 xml 中的标签名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8635446/

相关文章:

php - json_decode() 返回错误 "Notice: Trying to get property of non-object"

php - --dump-sql 做什么?

php - 允许忽略 PHP 日期中的天或月

php - 保存邮箱密码? - 这安全吗?

php - curl 错误 : unable to set private key file: 'test.pem' type PEM

php - "slash before every quote"问题

php - MySQL触发器无法调用

PHP Zip 文件为空?

php - 退回邮件解析

php - 仅选择一个主题和附件