SimpleXML属性到数组

时间:2012-07-11 19:17:27

标签: php arrays attributes simplexml

有没有更优雅的方法将SimpleXML属性转义为数组?

$result = $xml->xpath( $xpath );
$element = $result[ 0 ];
$attributes = (array) $element->attributes();
$attributes = $attributes[ '@attributes' ];

我真的不想仅仅为了提取键/值对而循环它。我只需要将它放入一个数组然后传递它。我原以为attributes()默认会这样做,或者至少给出选项。但我甚至无法在任何地方找到上述解决方案,我必须自己解决这个问题。我是不是太复杂了这个或什么?

修改

我仍然使用上面的脚本,直到我确定访问@attributes数组是否安全。

5 个答案:

答案 0 :(得分:47)

更优雅的方式;它在不使用 $ attributes ['@attributes'] 的情况下为您提供相同的结果:

$attributes = current($element->attributes());

答案 1 :(得分:11)

不要直接阅读'@attributes'属性,这是供内部使用的。无论如何,attributes()已经可以用作数组而无需“转换”为真实数组。

例如:

<?php
$xml = '<xml><test><a a="b" r="x" q="v" /></test><b/></xml>';
$x = new SimpleXMLElement($xml);

$attr = $x->test[0]->a[0]->attributes();
echo $attr['a']; // "b"

如果你想让它成为一个“真正的”数组,你将不得不循环:

$attrArray = array();
$attr = $x->test[0]->a[0]->attributes();

foreach($attr as $key=>$val){
    $attrArray[(string)$key] = (string)$val;
}

答案 2 :(得分:3)

您可以将整个xml文档转换为数组:

$array = json_decode(json_encode((array) simplexml_load_string("<response>{$xml}</response>")), true);

有关详细信息,请参阅:https://github.com/gaarf/XML-string-to-PHP-array

答案 3 :(得分:0)

我认为你必须循环。一旦你读了xml,就可以把它变成数组。

<?php
function objectsIntoArray($arrObjData, $arrSkipIndices = array())
{
$arrData = array();

// if input is object, convert into array
if (is_object($arrObjData)) {
    $arrObjData = get_object_vars($arrObjData);
}

if (is_array($arrObjData)) {
    foreach ($arrObjData as $index => $value) {
        if (is_object($value) || is_array($value)) {
            $value = objectsIntoArray($value, $arrSkipIndices); // recursive call
        }
        if (in_array($index, $arrSkipIndices)) {
            continue;
        }
        $arrData[$index] = $value;
    }
}
return $arrData;
}

$xmlStr = file_get_contents($xml_file);
$xmlObj = simplexml_load_string($xmlStr);
$arrXml = objectsIntoArray($xmlObj);

foreach($arrXml as $attr)
  foreach($attr as $key->$val){
 if($key == '@attributes') ....
}

答案 4 :(得分:0)

对我来说,方法工作

function xmlToArray(SimpleXMLElement $xml)
{
    $parser = function (SimpleXMLElement $xml, array $collection = []) use (&$parser) {
        $nodes = $xml->children();
        $attributes = $xml->attributes();

        if (0 !== count($attributes)) {
            foreach ($attributes as $attrName => $attrValue) {
                $collection['@attributes'][$attrName] = strval($attrValue);
            }
        }

        if (0 === $nodes->count()) {
            if($xml->attributes())
            {
                $collection['value'] = strval($xml);
            }
            else
            {
                $collection = strval($xml);
            }
            return $collection;
        }

        foreach ($nodes as $nodeName => $nodeValue) {
            if (count($nodeValue->xpath('../' . $nodeName)) < 2) {
                $collection[$nodeName] = $parser($nodeValue);
                continue;
            }

            $collection[$nodeName][] = $parser($nodeValue);
        }

        return $collection;
    };

    return [
        $xml->getName() => $parser($xml)
    ];
}

这也为我提供了所有属性,这是我从任何其他方法中都无法获得的。

相关问题