22

SimpleXML属性を配列にエスケープするためのより洗練された方法はありますか?

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

キーと値のペアを抽出するためだけにループする必要はありません。必要なのは、それを配列に入れてから渡すことだけです。私はそれをデフォルトでやっただろうと思っていattributes()たでしょう、あるいは少なくともオプションを与えられたでしょう。しかし、私はどこにも上記の解決策を見つけることができませんでした、私は自分でそれを理解しなければなりませんでした。私はこれか何かを複雑にしすぎていますか?

編集:

@attributes配列へのアクセスが安全かどうかが確実にわかるまで、上記のスクリプトを使用しています。

4

5 に答える 5

60

よりエレガントな方法。$ attributes ['@attributes']を使用しなくても同じ結果が得られます:

$attributes = current($element->attributes());
于 2012-12-03T05:24:16.893 に答える
12

'@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;
}
于 2012-07-11T19:32:14.003 に答える
4

xmlドキュメント全体を配列に変換できます。

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

詳細については、https ://github.com/gaarf/XML-string-to-PHP-arrayを参照してください。

于 2014-05-13T08:31:08.903 に答える
1

私にとって以下の方法はうまくいきました

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)
    ];
}

これにより、他の方法では取得できなかったすべての属性も提供されます。

于 2018-06-13T12:15:13.800 に答える
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') ....
}
于 2012-07-11T19:41:20.340 に答える