选择除特定形式Xml对象之外的元素

时间:2013-05-22 06:24:06

标签: php xml parsing

我正在解析下面给出的数组。使用foreach循环使用td。现在我想选择[@attributes]以外的值。我不能特意使用a或p,因为它们会改变对象。

我怎样才能实现这个目标?

[0] => SimpleXMLElement Object
    (
        [th] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [rowspan] => 2
                        [scope] => row
                    )

                [p] => Memory
            )

        [td] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [@attributes] => Array
                            (
                                [class] => ttl
                            )

                        [a] => Card slot
                    )

                [1] => SimpleXMLElement Object
                    (
                        [@attributes] => Array
                            (
                                [class] => nfo
                            )

                        [p] => No
                    )

            )

    )

希望解决方案能够在php中运行。

1 个答案:

答案 0 :(得分:2)

尝试以下

<?php
foreach($td as $element)
{
    foreach($element as $key => $value)
    {
        if(!preg_match("/@/", $key) && !is_array($value))
            echo $element[$key];
    }
}
?>